jackana3
jackana3

Reputation: 27

Combination of two SQL queries using IN statement

I have the sql query:

SELECT DISTINCT project_id 
FROM projects 
WHERE  (meta_key = "Project Name" AND meta_value LIKE '%t%' )

This returns

project_id
251
299
302
319
317
307
310
312
371
364
365
366
368

I have another query:

SELECT DISTINCT project_id
FROM tags
WHERE (tag LIKE '%t%')

This returns:

project_id
251

I try to combine the two statements with an IN statement in order to get there intersection.

SELECT DISTINCT project_id 
FROM projects 
WHERE (meta_key = "Project Name" AND meta_value LIKE '%t%' )
IN (SELECT DISTINCT project_id
    FROM tags
    WHERE (tag LIKE '%t%'))

However, I get nothing for the result.

project_id

I think I should get

project_id
251

Am I using the IN statement correctly here? If not which tool should I be using to accomplish this task. Thank you.

Upvotes: 0

Views: 69

Answers (1)

Parfait
Parfait

Reputation: 107652

Simply assign project_id to the IN clause expression. Right now, IN() evaluates the boolean logic of other WHERE condition and False or True will never be equivalent to a three-digit integer.

...
WHERE (meta_key = 'Project Name' AND meta_value LIKE '%t%' )
AND project_id IN (SELECT DISTINCT project_id
                   FROM tags
                   WHERE (tag LIKE '%t%'))

Upvotes: 2

Related Questions