douhan8581 2019-06-10 18:08
浏览 589
已采纳

使用多个where子句选择多个查询

I have two select queries I want to combine for faster performance. I'm a bit confused as I'm not sure if I could use a JOIN or LEFT JOIN or something else to make it faster.

Here are the two queries I want to combine:

Query 1:

SELECT id FROM users WHERE email = :email LIMIT 1

Then, if that row exists from query 1, then run query 2:

SELECT * FROM settings WHERE id = :id_from_query_1

thank you for any kind of help!

  • 写回答

5条回答 默认 最新

  • dougehe2022 2019-06-10 18:17
    关注

    When making a join query you can specify what column to join on. In this case it would be:

    select users.id, settings.*
    from users
    join settings on users.id = settings.id
    

    You could also use a double from clause which I think results in the same speed background wise that would look like:

    select users.id, settings.*
    from users, settings
    where users.id = settings.id
    

    If proper indexes are set a join should be faster than 2 selects. Ofcourse there are many factors when it comes to speed like load, network, what is cached and what isn't etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题