1

我有一个 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'];
}

如何打印出该行来自哪个表?

4

2 回答 2

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'];
}
于 2012-10-17T02:58:57.013 回答
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'];
}

我无法对他的帖子发表评论。

于 2012-10-17T03:30:58.647 回答