Search code examples
mysqldummy-datainner-query

How to specify a dummy column in outer queries using MySQL?


I need to perform the following query:

SELECT ID FROM tbl_a WHERE (fld_merchID, 'something') IN
(SELECT MerchID, MAX(Priority) FROM tbl_b WHERE Name LIKE 'AAA.com')

as you can image, there's no 'Priority' field in tbl_a. (I need to select the MerchID with the higher Priority value).

That query is returning empty and it shouldn't.

I'm not sure how to do it.

Thanks!


Solution

  • If you want the one with the max priority, then you need to change your subquery pretty significantly to choose the MerchID with the highest priority:

    SELECT ID FROM tbl_a WHERE fld_merchID IN (
      SELECT DISTINCT MerchID
      FROM tbl_b
      INNER JOIN (
        SELECT Name, MAX(Priority) as Priority
        FROM tbl_b 
        WHERE Name LIKE 'AAA.com'
        GROUP BY Name
      ) mx ON mx.Name = tbl_b.Name AND mx.Priority = tbl_b.Priority
    )