0
I have two tables, one contains the chave primária
, and the other takes this key in the column post_id
. In the second column, she can repeat several times with the same post_id
.
I need to take the second table, all distinct (single) lines. But it should delete the ID you have in the column meta_key
the word "tracking_code"
SELECT DISTINCT *
FROM wp_posts p
INNER JOIN wp_postmeta m ON p.ID = m.post_id
WHERE
(p.post_status = "wc-processing" AND p.post_type = "shop_order") AND m.meta_key != "tracking_code"
I have 2 problems the code above:
1º - I need DISTINCT to bring all columns wp_posts.
2º - The use of DISTINCT in a specific column, in WHERE eu "tracking_code". But as there are others post_id
repeated and not containing the word tracking_code
, he ends up returning the primary key. I needed something like: have the word "tracking_code", cannot select this ID, regardless of other repeat lines not having.
Table wp_posts
_____________________________________
| ID | post_status | post_type |
| 1 | wc-processing | shop_order |
| 2 | wc-processing | shop_order |
| 3 | wc-fail | shop_order |
| 4 | wc-processing | shop_order |
Table wp_postmeta
_______________________________________________________
| post_id | meta_key | meta_value |
| 1 | _correios_tracking_code | PM353535353BR |
| 1 | _codex | Elite |
| 2 | _correios_tracking_code | |
| 2 | _codex | Elite |
| 4 | _teste | Teste |
| 4 | _codex | Elite |
"I have two tables, one contains the primary key, and the other takes this key in the post_id column. In the second column, it can repeat several times with the same post_id.", it is not easier to post the table template? :)
– Ricardo Pontual
"But it should delete the ID you have in the meta_key column the word "tracking_code" can try a
where not exists
with a subquery wants to return these Ids– Ricardo Pontual
@Ricardopontual Updated, grateful.
– abcd