Select separate rows in SQL by adding deletion with different keys

Asked

Viewed 47 times

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? :)

  • "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

  • @Ricardopontual Updated, grateful.

2 answers

0

See if with the NOT EXISTS clause applied as the query below resolves.

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
NOT EXISTS (SELECT 1 FROM wp_postmeta  m2  WHERE m2.meta_key = m.meta_key 
AND m2.meta_key = "tracking_code")
  • It was almost. I had to change the to: m2.post_id = m.post_id instead of meta_key . The only problem is DISTINCT *, it does not work, I have to change to p.ID . There is some way gave to bring all the distinct columns, without setting one by one?

  • I mistook meta_key by id. I don’t know if it would be possible for a distinct command without determining columns.

  • I get it. Is it, when I implement in php, it will resume the whole row? or just the ID column? Is it possible to make a new select with these "DISTINCT p.ID" Ids, searching for the rest of the columns? because in my case they can be dynamic.

  • You could do this. Once you have p.id it would be possible through a new query to fetch the other columns.

0


The final result was:

select * from wp_posts u where u.ID in (
SELECT DISTINCT(p.ID)
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 
 NOT EXISTS 
(SELECT 1 FROM wp_postmeta  m2  WHERE m2.post_id = m.post_id  AND m2.meta_key = "tracking_code" )
 )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.