我有2个表在UNION的帮助下连接在一起,并使用单个while循环显示记录。
问题: 现在问题是我想分别从表(1)和表(2)显示记录,但条件只使用单个while循环。
这是SQL:`
$sql = "(SELECT * FROM subject_first WHERE date BETWEEN '$user_from' AND '$user_to') UNION (SELECT * FROM subject_second WHERE date BETWEEN '$user_from' AND '$user_to')";
有没有办法做到这一点?。容易说,但我是PHP的新手。 任何建议表示赞赏。
答案 0 :(得分:0)
只需在查询的两边添加一个字段:
(SELECT *, 'subject_first' as `table` FROM subject_first WHERE date BETWEEN '$user_from' AND '$user_to')
UNION
(SELECT *, 'subject_second' as `table` FROM subject_second WHERE date BETWEEN '$user_from' AND '$user_to')
ORDER BY `table`
我按部分添加了订单,因此您可以插入<br>
或其他任何内容来分割结果。