2017-02-27 28 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