使用子查询从两个表中选择
我有一个包含两个表用户和订单的数据库
users: user_id(primary), fname, lname, email, password, gender
orders: order_no(primary), user_id(foriegn), beans, type, extras, city
订单表仅包含提交订单的用户。
我需要知道如何选择所有用户及其订单数量(如果他们有订单或没有订单)。
作为答案发布的附加信息...
用户表:
user_id fname lname email password gender
1 a aa aaa 123 m
2 b bb bbb 34 f
订单表:
order_no user_id bean type extras city
1 2 s d rr ggg
2 2 s d rr ggg
如何选择所有用户表列以及 a 和 b 的订单计数,因此新表将是:
user_id fname lname email password gender orders_count
1 a aa aaa 123 m 0
2 b bb bbb 34 f 2
I have a database with two tables users and orders
users: user_id(primary), fname, lname, email, password, gender
orders: order_no(primary), user_id(foriegn), beans, type, extras, city
The orders table have only users who submitted orders.
I need to know how to select all users with the count of their orders if they have orders or don't.
Additional Info posted as an answer....
users table:
user_id fname lname email password gender
1 a aa aaa 123 m
2 b bb bbb 34 f
orders table:
order_no user_id bean type extras city
1 2 s d rr ggg
2 2 s d rr ggg
how to select all users table columns plus orders count for a and b so the new table will be:
user_id fname lname email password gender orders_count
1 a aa aaa 123 m 0
2 b bb bbb 34 f 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据更新的信息,使用:
Based on the updated information, use:
您的更新基本上是在请求 pcofre 的答案已经为您提供的内容。您只需将其他必需的列添加到您的
选择
列表中,并为聚合提供一个列别名。Your update is basically requesting what pcofre's answer already gives you. You just need to add the additional required columns to your
select
list and provide a column alias for the aggregate.