我有一个SELECT语句,其中我加入了2个单独的表。
$result = mysql_query('SELECT * FROM (SELECT * FROM gslil0009) as table1 UNION SELECT * FROM (SELECT * FROM gslil0028) as table2' . ' ORDER BY lname');
while($row = mysql_fetch_array($result)) {
echo $row['fname'];
}
如何打印出来自哪个表?
答案 0 :(得分:2)
$result = mysql_query('SELECT *,'tbl1' FROM (SELECT * FROM gslil0009) as table1
UNION SELECT *,'tbl2' FROM (SELECT * FROM gslil0028) as table2'
. ' ORDER BY lname');
while($row = mysql_fetch_array($result)) {
echo $row['fname'];
echo $row['tbl1'];
}
答案 1 :(得分:0)
我使用与sel相同的解决方案只需添加反斜杠以避免错误并为表名添加名称。
$result = mysql_query('SELECT *,\'table1\' AS tablename FROM (SELECT * FROM gslil0009) as table1
UNION SELECT *,\'table2\' AS tablename FROM (SELECT * FROM gslil0028) as table2'
. ' ORDER BY lname');
while($row = mysql_fetch_array($result)) {
echo $row['fname'];
echo $row['tablename'];
}
我无法在他的帖子上添加评论。