如何使用select查询查找表名

时间:2014-04-13 09:10:23

标签: mysql vb.net

SELECT product FROM Table_name_1 
WHERE id = '% textbox1.text %'  
UNION  
SELECT product FROM Table_name_2 
WHERE id = '% textbox1.text %'  
UNION  
SELECT product FROM Table_name_3
WHERE id = '% textbox1.text %' 

通过此代码,我可以获得产品。但是我怎么知道这个结果从哪个表中取出?所以我也想知道表名。

1 个答案:

答案 0 :(得分:0)

您还可以选择常量值并为其指定列名称。

SELECT product, 'Table_name_1' as table FROM Table_name_1 WHERE id = '% textbox1.text %'
UNION
SELECT product, 'Table_name_2' as table FROM Table_name_2 WHERE id = '% textbox1.text %' 
UNION
SELECT product, 'Table_name_3' as table FROM Table_name_3 WHERE id = '% textbox1.text %'