如何组合2个表按日期排序并将它们输出到列表中? 这就是我现在所拥有的。
列表中的日期应为此格式2015-06-27 23:00
SELECT date, name, quantity, FROM downloads ORDER BY(`Date`) DESC
SELECT date, name, quantity, price, FROM sales ORDER BY(`Date`) DESC
$result_sales = $mysqli->query($sql_sales);
while($row = $result_sales->fetch_assoc()){
$sale = rown["quantity"];
$name = row["name"];
$date_str = strtotime($row["date"]);
$year = date('Y', $date_str);
$month = date('n', $date_str) - 1;
$day = date('j', $date_str);
$hour = date('H', $date_str);
$minute = date('i', $date_str);
$ans_sales = " " . sprintf('%s, %s, %s, %s, %s',$year,$month,$day,$hour,minute) . " - ". $name ." - " . $sale . " - Sale Added";
echo $ans_sales . ",\r\n";
}
答案 0 :(得分:1)
删除,
附近的FROM
。
SELECT date, name, quantity, price, FROM sales ORDER BY(`Date`) DESC
^^
对于组合两个表使用UNION ALL
(SELECT date, name, quantity FROM downloads ORDER BY `Date` DESC)
UNION ALL
(SELECT date, name, quantity, price FROM sales ORDER BY `Date` DESC)
ORDER BY `Date` DESC
答案 1 :(得分:1)
SELECT date, name, quantity, 0 as price FROM downloads
union all
SELECT date, name, quantity, price FROM sales
ORDER BY(`Date`) DESC
答案 2 :(得分:1)
使用查询
创建一个mysql VIEW'view_all'SELECT
date,
name,
quantity,
null as price
FROM downloads
UNION ALL
SELECT
date,
name,
quantity,
price
FROM sales
然后执行
SELECT * FROM'view_all'ORDER BY(date
)DESC;