duangou2028 2015-11-15 15:32
浏览 32
已采纳

解释MySQL查询

Some days ago I asked here for a solution to retrieve a query which gets values from two diferent tables. The question was asked quite fast, thanks to this awesome community here... But continuing: I used the query and it worked perfectly, but I don't understand it. The query is following:

SELECT e.*, u.Username 
FROM Entries AS e 
LEFT JOIN Users AS u ON u.ID = e.User 
WHERE e.Category = 'SomeCategory' 
LIMIT 0, 10

Can anyone explain what each "step" of the query does? "WHERE","LIMIT","SELECT" and "FROM" I know, but the others get quite complicated for me.

Thanks

EDIT: LIMIT 10,0 changed to LIMIT 0,10. I written it the wrong way... Sorry about that.

  • 写回答

1条回答 默认 最新

  • doumei1926 2015-11-15 15:36
    关注
    SELECT e.*, u.Username 
    FROM Entries AS e 
    LEFT JOIN Users AS u ON u.ID = e.User 
    WHERE e.Category = 'SomeCategory' 
    LIMIT 10, 0;   -- are you sure about it???
    

    How it works:

    1. Get Entries table and "rename" it to e (add alias)
    2. Get Users table and "rename" it to u
    3. Join (outer) both tables based on ID column from Entries and User from Users (for non matched rows from Users set NULL)
    4. Filter out records that value in Category column is equal 'SomeCategory'
    5. Skip first 10 rows and get 0 rows
    6. Show every column from Entries and Username column from Users

    Warning

    Without explicit ORDER BY you can get different resultsets between executions. Why you want to return empty resultset LIMIT offset, row_count?

    EDIT:

    After your edit 5. should be Skip first 0 rows and get 10 rows

    LIMIT 0, 10 is the same as LIMIT 10

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用