我正在尝试组合一个 MySQL 查询,它将搜索 2 个表 table1 和 table2 并选择任一表中不到 30 天的所有结果。
我的桌子:
table1
id | user_id | name | age | date |
table2
id | user_id | name | age | date | account | sortcode |
我正在呼应这样的结果:
<?php require_once 'config.php'; ?>
<?php
$table1 = 'supplier_bank_details';
$table2 = 'supplier_invoices';
$query = "SELECT *, $table1 as TABLE from $table1 where
date > NOW() - INTERVAL 30 DAY and user_id = '{$_SESSION['id']}' ORDER BY date DESC
UNION
SELECT *, $table2 as TABLE from $table2 where
date > NOW() - INTERVAL 30 DAY and user_id = '{$_SESSION['id']}' ORDER BY date DESC";
$result = mysql_query($query) or die( mysql_error() );
while($row = mysql_num_fields($result)){
if($result === $table1) {
echo 'this result is from table1';
echo $row['name'];
echo $row['age'];
}else{
if($result === $table2) {
echo 'this result is from table2';
echo $row['name'];
echo $row['age'];
} } }
?>
所以基本上我试图设置一个条件来检查结果来自哪个表,并回显'结果来自表 1/2'以及该表中的值。
有谁知道我怎么能做到这一点,因为我对 MySQL 查询很陌生。提前致谢,