简体   繁体   English

PHP&MySQL - 如何确定结果来自哪个表

[英]PHP&MySQL - How to determine which table a result is from

I have the following code that searches two different tables in my database.我有以下代码在我的数据库中搜索两个不同的表。

$defaultQuery = mysqli_query($con, "SELECT fullname, company, id FROM employees WHERE company = '$topCID' AND `id` != '$eid' UNION ALL SELECT fullname, company, id FROM users WHERE company = '$topCID' ORDER BY RAND() LIMIT 6");

Is there a way to create an if statement in my results to say "IF this result is from EMPLOYEES, ECHO the letter "e". IF this result is from USERS, echo the letter "u"."?有没有办法在我的结果中创建一个 if 语句说“如果这个结果来自 EMPLOYEES,ECHO 字母“e”。如果这个结果来自 USERS,回应字母“u”。“?

As I said, just ad an extra column:正如我所说,只需添加一个额外的列:

SELECT fullname, company, id, 'e' as `Source`
FROM employees 
WHERE company = '$topCID' AND `id` != '$eid' 
UNION ALL 
SELECT fullname, company, id, 'u' 
FROM users 
WHERE company = '$topCID' 
ORDER BY RAND() 
LIMIT 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM