doubing3662 2014-08-26 17:25
浏览 117
已采纳

多个选择查询MySQL没有while循环

I have two different tables where I need to fetch the list of store ids from one table and then find the list of coupons for those store ids.

Currently,

SELECT `storename` FROM `stores` where `brandname` = 21 

This will return something like

Store 1
Store 2
Store 3
Store 4

And I need to to run another query like

SELECT * FROM `coupons` where `storename` = {{All these stores}}

I can't use while loops because, the number of stores comes from first query can't be determined and the the output I want was not coming as expected while using while loop as I am trying to do something like

while(first query output get storename)
{

do query here

   while(second query output get all coupons per store)
   {
      // All coupons display here.
   }

}

This is making quite complicated as well, is there anyway that I can tweak my SQL query and get results easily?

Thanks

  • 写回答

2条回答 默认 最新

  • douwen5833 2014-08-26 17:28
    关注

    you can use this query:

    SELECT * FROM `coupons`
    where `storename` IN (
        SELECT `storename` FROM `stores` where `brandname` = 21);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部