Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql &引用;“分类”;或;标记“;选择查询_Mysql_Sql_Select - Fatal编程技术网

Mysql &引用;“分类”;或;标记“;选择查询

Mysql &引用;“分类”;或;标记“;选择查询,mysql,sql,select,Mysql,Sql,Select,我有一个从多个数据库中选择列“auth”=mad的查询: $result = mysql_query("SELECT * FROM test.1 WHERE auth = 'mad' LIMIT 1 UNION SELECT * FROM test.2 WHERE auth = 'mad' LIMIT 1 UNION SELECT * FROM test.3 WHERE auth = 'mad' LIMIT 1"); 我如何标记或分类测试结果。1;测试2还是测试3 非常感谢

我有一个从多个数据库中选择列“auth”=mad的查询:

$result = mysql_query("SELECT * FROM test.1 WHERE auth = 'mad' LIMIT 1 
    UNION SELECT * FROM test.2 WHERE auth = 'mad' LIMIT 1 
    UNION SELECT * FROM test.3 WHERE auth = 'mad' LIMIT 1");
我如何标记或分类测试结果。1;测试2还是测试3


非常感谢。

将表名添加到结果中:

$result = mysql_query("SELECT *, 'test.1' FROM test.1 WHERE auth = 'mad' LIMIT 1 
                  UNION SELECT *, 'test.2' FROM test.2 WHERE auth = 'mad' LIMIT 1 
                  UNION SELECT *, 'test.3' FROM test.3 WHERE auth = 'mad' LIMIT 1");

如果我理解正确,您只需添加一列。此外,最好使用
union all
而不是
union
,因为
union
也会花费额外的精力来消除重复项:

SELECT 1 as test, t1.* FROM test.1 t1 WHERE auth = 'mad' LIMIT 1 
    UNION ALL
SELECT 2 as test, t2.* FROM test.2 t2 WHERE auth = 'mad' LIMIT 1 
    UNION ALL
SELECT 3 as test, t3.* FROM test.3 t3 WHERE auth = 'mad' LIMIT 1;
SELECT 1 as test, t1.* FROM test.1 t1 WHERE auth = 'mad' LIMIT 1 
    UNION ALL
SELECT 2 as test, t2.* FROM test.2 t2 WHERE auth = 'mad' LIMIT 1 
    UNION ALL
SELECT 3 as test, t3.* FROM test.3 t3 WHERE auth = 'mad' LIMIT 1;