Let’s take parts. Basically your query would look like this:
SELECT wp_postmeta.post_id,
wp_posts.post_title
FROM wp_postmeta
INNER JOIN wp_posts on wp_posts.id = wp_postmeta.post_id
The problem is that, the same table "wp_postmeta" contains the data of the same post in different records, this requires making a join
with the table itself multiple times, to objter name, email and value, something like this:
INNER JOIN wp_postmeta wp_name on wp_postmeta.post_id = wp_name.post_id
INNER JOIN wp_postmeta wp_email on wp_postmeta.post_id = wp_email.post_id
INNER JOIN wp_postmeta wp_adicional on wp_postmeta.post_id = wp_adicional.post_id
Note that since they are multiple joins with different purposes, each has one alias different. Also, you need to filter by "meta_key" to get the correct values, then we can add the clause WHERE
, thus:
WHERE wp_name.meta_key = '_appointment_guest_name'
AND wp_email.meta_key = '_appointment_guest_email'
AND wp_adicional.meta_key = '_cf_meta_value'
Here I used the different ones alias to filter what each must return.
Finally, to avoid duplicate results, let’s group by "post_id". The complete query looks like this:
SELECT wp_postmeta.post_id,
wp_name.meta_value as nome,
wp_email.meta_value as email,
wp_adicional.meta_value as adicional,
wp_posts.post_title
FROM wp_postmeta
INNER JOIN wp_postmeta wp_name on wp_postmeta.post_id = wp_name.post_id
INNER JOIN wp_postmeta wp_email on wp_postmeta.post_id = wp_email.post_id
INNER JOIN wp_postmeta wp_adicional on wp_postmeta.post_id = wp_adicional.post_id
INNER JOIN wp_posts on wp_posts.id = wp_postmeta.post_id
WHERE wp_name.meta_key = '_appointment_guest_name'
AND wp_email.meta_key = '_appointment_guest_email'
AND wp_adicional.meta_key = '_cf_meta_value'
GROUP BY wp_postmeta.post_id
Can be seen working here: http://sqlfiddle.com/#! 9/860125/1
Finally, put this in a view:
CREATE VIEW MinhaView AS
SELECT wp_postmeta.post_id,
wp_name.meta_value as nome,
wp_email.meta_value as email,
wp_adicional.meta_value as adicional,
wp_posts.post_title
FROM wp_postmeta
INNER JOIN wp_postmeta wp_name on wp_postmeta.post_id = wp_name.post_id
INNER JOIN wp_postmeta wp_email on wp_postmeta.post_id = wp_email.post_id
INNER JOIN wp_postmeta wp_adicional on wp_postmeta.post_id = wp_adicional.post_id
INNER JOIN wp_posts on wp_posts.id = wp_postmeta.post_id
WHERE wp_name.meta_key = '_appointment_guest_name'
AND wp_email.meta_key = '_appointment_guest_email'
AND wp_adicional.meta_key = '_cf_meta_value'
GROUP BY wp_postmeta.post_id;
And then make a SELECT * FROM MinhaView
You can see working with the view
here: http://sqlfiddle.com/#! 9/7a1cfe/2
a suggestion, instead of having duplicated data, couldn’t you create a view? would always be up to date without the need to occupy space or duplicate data
– Ricardo Pontual
@Ricardopunctual I understand, could you tell me where to start? rs The idea of duplicating the data is because wordpress uses this table to save all the information, even those that are from another plugin and I wanted to filter only the information of this plugin to be able to generate a PDF with the data. Thanks :D
– Richard Lucas
Yes I can Richard, can assemble a query with the results you need, using the table
wp_postmeta
? Then just save this query as aview
– Ricardo Pontual
Ricardo, I did the query but I have a question. I am forwarding a link with a print of the bank table and I need it to take only the information that is pointed out in the print. OBS: I’ll leave the query at the end of the question. Follow print link: https://ibb.co/CJVqKzM
– Richard Lucas
You can give a
DESCRIBE table_name;
to add to your post? Still, you can put a table row even if changing the data in the post?– Paulo Marques
Paulo, I think I understood your question. I put DESCRIBE in the post and in the comment above, I put a print of the table data.
– Richard Lucas
I put a very complete and explanatory example using a view
– Ricardo Pontual