【问题标题】:Only one row is shown, when using in clause in SQL在 SQL 中使用 in 子句时,只显示一行
【发布时间】:2017-02-27 07:22:51
【问题描述】:

查询 1:

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

输出:

查询 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` "

输出:

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

【问题讨论】:

标签: php mysql where-in


【解决方案1】:

尝试使用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` 

【讨论】:

  • @SaurabhRanjan FIND_IN_SET 是非 1NF 数据库表的救星。 en.wikipedia.org/wiki/First_normal_form如果您有权限/能力修改表,请将csv列移到单独的表中
  • 首先谢谢你。你给了我另一种方法来解决这个问题。
    你能告诉我“FIND_IN_SET是非1NF数据库表的救星”是什么意思
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-16
  • 2010-12-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-29
  • 2011-03-21
相关资源
最近更新 更多