dpbz14739 2019-03-14 20:10
浏览 34

SQL:如何在SQL中过滤(WHERE)连接(ON)表?

I have two tables (users and posts) and I want to write out all posts (among other things) by one user. I'm thinking I should use a JOIN and WHERE but I get an error for using WHERE.

This is my code:

SELECT username, post, joinDate, title, info FROM users 
    WHERE userId='18' 
    JOIN posts ON users.userId=posts.userId 
    ORDER BY date DESC

I'm new to this and perhaps there is a better way but I can't figure it out atm. Thankful for all answers!

  • 写回答

3条回答 默认 最新

  • duancilan5124 2019-03-14 20:12
    关注

    The JOIN clause comes before the WHERE clause, after the FROM clause. First you join together all the tables you need, then you do your filtering with WHERE. Like this:

    SELECT username, post, joinDate, title, info
    FROM users 
    JOIN posts ON users.userId=posts.userId 
    WHERE users.userId='18'
    ORDER BY date DESC
    
    评论

报告相同问题?

悬赏问题

  • ¥15 vscode的问题提问
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM