好的查询从三个表中获取信息以获取帐户详细信息,从管理员,员工和客户端问题是我的,因为有人只存在于Admin_Agency,Agency_Employee或Client表中我最终得到了一堆列包含NULLS,见下文。
在这里,我想以某种方式组合以下列,以便没有NULL列:
Client.First_Name,Admin_Agency.Admin_First_Name和Admin_Agency.Admin_First_Name 进入First_Name
Client.Last_Name,Admin_Agency.Admin_Last_Name和Admin_Agency.Admin_Last_Name 进入Last_Name
最后所有列都命名为“Profile_Pic”。
可以这样做吗?
+--------------------+---------------------+------------+------------------+-----------------+-------------+------------+-----------+-------------+------------+-----------+-------------+
| User_Comment | Time_Stamp | Account_ID | Admin_First_Name | Admin_Last_Name | Profile_Pic | First_Name | Last_Name | Profile_Pic | First_Name | Last_Name | Profile_Pic |
+--------------------+---------------------+------------+------------------+-----------------+-------------+------------+-----------+-------------+------------+-----------+-------------+
| Lorem ipsum dolor | 2012-03-21 23:40:15 | 18 | Fredric | Beskoski | 002.jpg | NULL | NULL | NULL | NULL | NULL | NULL |
+--------------------+---------------------+------------+------------------+-----------------+-------------+------------+-----------+-------------+------------+-----------+-------------+
| Lorem ipsum dolor | 2012-04-21 23:40:05 | 20 | NULL | NULL | NULL | Alan | James | 032.jpg | NULL | NULL | NULL |
+--------------------+---------------------+------------+------------------+-----------------+-------------+------------+-----------+-------------+------------+-----------+-------------+
| Lorem ipsum dolor | 2012-05-24 21:40:12 | 21 | NULL | NULL | NULL | NULL | NULL | NULL | David | Calson | 044.jpg |
+--------------------+---------------------+------------+------------------+-----------------+-------------+------------+-----------+-------------+------------+-----------+-------------+
请注意,Admin_First_Name,Admin_Last_Name和Profile_Pic已填充,但其余为NULL。
查询:
SELECT
Comments.User_Comment, Comments.Time_Stamp, Account.Account_ID,
Admin_Agency.Admin_First_Name, Admin_Agency.Admin_Last_Name, Admin_Agency.Profile_Pic,
Agency_Employee.First_Name, Agency_Employee.Last_Name, Agency_Employee.Profile_Pic,
Client.First_Name, Client.Last_Name, Client.Profile_Pic
FROM Comments
LEFT JOIN Account ON (Account.Account_ID = Comments.Account_ID_FK4)
LEFT JOIN Admin_Agency ON (Account.Account_ID = Admin_Agency.Agency_ID)
LEFT JOIN Agency_Employee ON (Account.Account_ID = Agency_Employee.Employee_ID)
LEFT JOIN Client ON (Account.Account_ID = Client.Client_ID)
WHERE Comments.Design_ID_FK = 5
感谢您的帮助!
答案 0 :(得分:0)
左外连接或右外连接可以为空提供。
内部联接可能会减少行数,但没有一行会引入“join”中的任何空值。
答案 1 :(得分:0)
您应首先设置优先级以检查哪一个(来自客户端或管理员的值)。例如,如果要检查第一个值是否存在于客户端,如:
select if(Client.First_Name is Null,Admin_Agency.Admin_First_Name,Client.First_Name)
as first_name ,... //Same for other columns
From ...
Joins....
Where ...