是否可以在同一个查询中同时进行多个查询?
以下是我正在尝试做的一个例子。
我们有下表:
| userid | price | stock | description |
----------------------------------------
1 10.00 5 some text
2 25.00 2 some text
3 15.00 3 some text
4 35.00 2 some text
5 30.00 4 some text
我正在尝试的查询是:
因此HTML表格如下所示:
description | Min_Price | Max_Price | Price Set by userid 2 | 1st Price | 1st Stock | 2nd Price | 2nd Stock | 3rd Price | 3rd Stock
答案 0 :(得分:0)
MySQL Unions(http://dev.mysql.com/doc/refman/5.0/en/union.html)应该会让你走上正轨。
您也可以使用子选择来完成大部分操作,但这可能不是一个好主意。
答案 1 :(得分:0)
使用union或,将所有内容放在一行中,如下所示:
Select
(Select min(price) from table) as min_price,
(Select max(price) from table) as max_price,
....
答案 2 :(得分:0)
此解决方案需要许多“子查询”或连接。
你可能正在寻找类似的东西:
select t1.description,
t1.min_price,
t1.max_price,
t2.user_id_2_price
from
(select description, min(price) as min_price, max(price) as max_price from t group by description) t1
left join
(select price, description as user_id_2_price from t where userid = '2') t2
on
(t1.description = t2.description)
您可以根据需要添加任意数量的“左连接”