English 中文(简体)
从两个表格中选取,使用分卡
原标题:select from two tables using subquery

我有一个数据库,有两个表格用户和订单。

users: user_id(primary), fname, lname, email, password, gender

orders: order_no(primary), user_id(foriegn), beans, type, extras, city

订单表只有提交订单的用户。

我需要知道,如果所有用户有定购单或无定购单,如何选择其订单。

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

如何选择所有用户表栏,加上对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
问题回答
   select U.user_id,
          COUNT(O.user_id)
     from users U 
left join orders O on U.user_id=O.user_id
 group by U.user_id

根据最新资料,使用:

   SELECT u.*,
          COALESCE(x.orders_count, 0) AS orders_count
     FROM USERS u
LEFT JOIN (SELECT o.user_id, 
                  COUNT(*) AS orders_count
             FROM ORDERS o
         GROUP BY o.user_id) x ON x.user_id = u.user_id
 ORDER BY u.user_id

您的更新基本上要求以下网站:https://stackoverflow.com/questions/5533033/select- from-second-tables-using-subquery/5533065#5533065>pcofre s accreditation/a>。 你只需在<条码>电子数据清单中添加必要的栏目,并提供合计数的栏目。

SELECT U.user_id,
       U.fname,
       U.lname,
       U.email,
       U.password,
       U.gender,
       COUNT(O.user_id) AS orders_count
FROM   users U
       LEFT JOIN orders O
         ON U.user_id = O.user_id
GROUP  BY U.user_id  /*<-- Don t need to add other users 
                          columns to GROUP BY in MySQL*/




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签