2

我正在寻找一个输出两个表的列的查询。我可能正在寻找加入,但如果我加入左表或右表,则可能不会返回值。我曾想过使用 UNION 关键字,但它会将列重命名为一个名称。然后不知道哪一栏有什么。

我希望我的查询选择两个表中的列,但不在乎任何一个表中是否存在与用户名匹配的内容,或者两者都不存在。我只是希望所有没有值的列都在那里。

SELECT *
FROM client_table
JOIN staff_table 
ON client_table.username = staff_table.username
WHERE client_table.username = " . mysql_real_escape_string($gotMyUser) . " 
OR staff_table " . mysql_real_escape_string($gotMyUser) . " LIMIT 1

更新:

这是查询的输出

Array
(
//FIRST TABLE
[client_id] => 332058
[username] => jake
[firstname] => jake
[lastname] => ****
[email] => *****
[phone] => 
[phone_ext] => 
[mobile] => 
[department] => 
[is_active] => 1
[default_site] => 0
[google] => 

//SECOND TABLE
[staff_id] => 
[group_id] => 
[dept_id] => 
[passwd] => 
[signature] => 
[isactive] => 
[isadmin] => 
[isvisible] => 
[onvacation] => 
[daylight_saving] => 
[append_signature] => 
[change_passwd] => 
[timezone_offset] => 
[max_page_size] => 
[auto_refresh_rate] => 
[created] => 
[lastlogin] => 
[updated] => 

)

这正是我想要的,因为我想知道用户是否有员工 ID 或客户 ID,但取决于我执行的连接(左右或完整),它可能不会返回任何结果。我希望每次执行查询时结果都包含所有这些列,然后进行测试:

if($array['client_id']){
//do stuff with client
}

else if($array['staff_id']){
//do stuff with staff
}
4

5 回答 5

2

正如 Julius Davies 所建议的那样,这个FULL OUTER JOIN概念正是您所需要的。但是,由于他们在 MySQL 中没有这些,因此您LEFT JOIN需要UNION使用RIGHT JOIN.

示例(请注意,它是在 2 个块中重复的相同查询,一个使用右外连接,另一个使用左外连接):

SELECT *
FROM client_table
LEFT OUTER JOIN staff_table 
ON client_table.username = staff_table.username
WHERE client_table.username = " . mysql_real_escape_string($gotMyUser) . " 
OR staff_table " . mysql_real_escape_string($gotMyUser) . " 

UNION


SELECT *
FROM client_table
RIGHT OUTER JOIN staff_table 
ON client_table.username = staff_table.username
WHERE client_table.username = " . mysql_real_escape_string($gotMyUser) . " 
OR staff_table " . mysql_real_escape_string($gotMyUser) . " 

看到这个问题

于 2013-01-15T18:18:11.150 回答
1

我认为您想要一个union all,但您必须正确获取列名:

SELECT 'client_table' as table_name, 
       client_id, username, firstname, lastname,  email, phone, phone_ext,mobile,department,is_active,default_site, google,
       NULL as staff_id, NULL as group_id, NULL as dept_id, NULL as passwd, NULL as signature, NULL as isactive, NULL as isadmin, NULL as isvisible, NULL as onvacation, NULL as daylight_saving,
       NULL as append_signature, NULL as change_passwd, NULL as timezone_offset, NULL as max_page_size, NULL as auto_refresh_rate, NULL as created, NULL as lastlogin, NULL as updated
FROM client_table
WHERE client_table.username = " . mysql_real_escape_string($gotMyUser) . " 
UNION all
select 'staff_table' as table_name,
       NULL as client_id, NULL as username, NULL as firstname, NULL as lastname,  NULL as email,
       NULL as phone, NULL as phone_ext, NULL as mobile, NULL as department, NULL as is_active, NULL as default_site, NULL as google,
       staff_id, group_id, dept_id, passwd, signature, isactive, isadmin, isvisible, onvacation, daylight_saving,
       append_signature, change_passwd, timezone_offset, max_page_size, auto_refresh_rate, created, lastlogin, updated
from staff_table 
where staff_table.username = " . mysql_real_escape_string($gotMyUser) . "
于 2013-01-15T18:53:23.203 回答
1

尝试将“JOIN”更改为“FULL OUTER JOIN”

http://en.wikipedia.org/wiki/Join_%28SQL%29#Full_outer_join

于 2013-01-15T18:15:30.050 回答
0

试试这个,mysql没有做全外连接的选项,所以你可以做左右连接的联合

SELECT *
FROM client_table
LEFT OUTER JOIN staff_table 
ON client_table.username = staff_table.username
WHERE client_table.username = " . mysql_real_escape_string($gotMyUser) . " 
OR staff_table " . mysql_real_escape_string($gotMyUser) . "
UNION
SELECT *
FROM client_table
RIGHT OUTER JOIN staff_table 
ON client_table.username = staff_table.username
WHERE client_table.username = " . mysql_real_escape_string($gotMyUser) . " 
OR staff_table " . mysql_real_escape_string($gotMyUser) . "
于 2013-01-15T18:17:53.433 回答
0

我认为您要做的是知道记录来自哪个表。如果您发布一些示例数据和您期望的结果,将会有所帮助。

如果我是正确的,并假设 client_table 和 staff_table 具有相同的结构,您可以执行以下操作:

SELECT 'client_table' as table_name, client_table.*
FROM client_table
WHERE client_table.username = " . mysql_real_escape_string($gotMyUser) . " 
UNION
select 'staff_table' as table_name, staff_table.*
from staff_table 
where staff_table.username = " . mysql_real_escape_string($gotMyUser) . "

通过添加 table_name,您可以知道数据的来源。

于 2013-01-15T18:21:55.200 回答