![](/img/trans.png)
[英]I have tables name as "expenses" and "orders" i want to get there total separately and then subtract orders total from expenses
[英]i have three tables and when i retrieve data from them i also want to get the table name too
$raw_results = mysql_query("SELECT sno,name,email,phone,smpc FROM existing_sales_details
WHERE (`sno` LIKE '%".$query."%') OR(`name` LIKE '%".$query."%') OR (`email` LIKE '%".$query."%') OR (`phone` LIKE '%".$query."%') OR (`smpc` LIKE '%".$query."%')
UNION
SELECT sno,name,email,phone,smpc FROM sale_details
WHERE (`sno` LIKE '%".$query."%') OR(`name` LIKE '%".$query."%') OR (`email` LIKE '%".$query."%') OR (`phone` LIKE '%".$query."%') OR (`smpc` LIKE '%".$query."%')
UNION
SELECT sno,name,email,phone,smpc FROM uncharge
WHERE (`sno` LIKE '%".$query."%') OR(`name` LIKE '%".$query."%') OR (`email` LIKE '%".$query."%') OR (`phone` LIKE '%".$query."%') OR (`smpc` LIKE '%".$query."%')")
or die(mysql_error());
我有三個表,當我從它們中檢索數據時,我也想獲取表名,以知道此數據屬於該表,我如何才能根據數據檢索表名,請建議我。
您可以輕松做到的是:
SELECT colum1, column2, 'tableName1' as tableName FROM tableName1
UNION
SELECT colum1, column2, 'tableName2' as tableName FROM tableName2
您始終可以在選擇中添加字符串或數字作為列。 每行都相同。
除了舊版mysql庫存在的問題外,返回表名實際上相當容易。
由於您使用的是UNION,實際上需要3個選擇,所以您要做的就是在每個選擇的末尾進行更改。
SELECT sno,name,email,phone,smpc
至
SELECT sno,name,email,phone,smpc,'XXXX' AS tablename
並在每個查詢中將XXXX替換為表名
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.