2017-02-27 26 views
-2

查詢1子句使用時:只有一排被示出,在SQL

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

的[通過VARCHAR列與IN()中的條件和int值返回所有行部分選擇]可能的複製(http://stackoverflow.com/questions/2064766/ select-by-varchar-column-in-in-in-in-condition-and-int-value-returns-all -r) –

回答

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` 
+0

@SaurabhRanjan FIND_IN_SET是非1NF數據庫表的救星。 https://en.wikipedia.org/wiki/First_normal_form如果您有權限/能力修改表格,請將csv列移入單獨的表 – mickmackusa

+0

首先感謝您的人。你給了我更多的方法來解決這個問題。
您能否告訴我您的意思是「FIND_IN_SET是非1NF數據庫表的救星」 – inrsaurabh