SELECTA中具有相同日期值的行,类别= 1

时间:2015-03-29 02:37:20

标签: mysql sql

我想选择当天出售的item_namecategory=1

SELECT 
    (SELECT item_name FROM item_out_tbl io 
    WHERE item_id=sa.item_id AND category_id=1) AS item_name, 
    date_sold 
FROM sales_tbl sa
WHERE date_sold >= CURDATE();

运行此查询后,我得到了一个

item_name   |   date_sold
--------------------------
   NULL     |  2015-03-29 05:16:15
  phone-ax2 |  2015-03-29 05:17:10

我只希望phone-ax2返回,因为item_out_tbl只有category_id=1中的phone-ax2。如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

Correlated sub-query转换为INNER JOIN

SELECT 
    io.item_name, 
    date_sold 
FROM sales_tbl sa
INNER JOIN item_out_tbl io 
ON item_id=sa.item_id 
WHERE sa.date_sold >= CURDATE()
AND io.category_id=1