在SQL中使用in子句时,只显示一行

时间:2017-02-27 07:22:52

标签: php mysql where-in

查询1:

select products 
from buyde_deal 
where displayflag = '1' 
and end_date> now() 
and start_date < now() limit 1 

输出:query 1 output

查询2:

SELECT id,productname ,cat_id ,subcat_id,shortdescription1,shortdescription2,shortdescription3 ,sellingprice,sellpricevat,mrp,regularprice,costprice,sku,qty,pweight,seller_id,shippingcost,color,size,discount 
FROM `buyde_product` 
WHERE id IN ( 
    select products 
    from buyde_deal 
    where displayflag = '1' 
    and end_date> now() 
    and start_date < now() ) 
ORDER BY `buyde_product`.`id` "

输出:query 2 output

如果我运行第二个查询,则只返回一条记录。我需要表1中的所有记录。

1 个答案:

答案 0 :(得分:0)

尝试使用find_in_set

SELECT id, 
       productname, 
       cat_id, 
       subcat_id, 
       shortdescription1, 
       shortdescription2, 
       shortdescription3, 
       sellingprice, 
       sellpricevat, 
       mrp, 
       regularprice, 
       costprice, 
       sku, 
       qty, 
       pweight, 
       seller_id, 
       shippingcost, 
       color, 
       size, 
       discount 
FROM   `buyde_product` 
       JOIN (SELECT products 
             FROM   buyde_deal 
             WHERE  displayflag = '1' 
                    AND end_date > Now() 
                    AND start_date < Now()) t 
         ON FIND_IN_SET(`buyde_product`.`id`, t.products) 
ORDER  BY `buyde_product`.`id`