mysql 在 php 中使用空值左连接

发布于 2024-12-02 19:55:45 字数 710 浏览 0 评论 0原文

示例场景;假设我正在寻找员工及其雇主

tblEmployee
eid | name
1  | james
2  | harry

tblCompany
cid | name
1  | ABC Ltd
2  | XYZ Corp

tblMappings
mid | eid | cid
1  | 1   | 2

James 被分配到 XYZ Corp 公司,但 Harry 没有分配。我想把所有员工都撤出来,不管他们有没有被分配;

SELECT * FROM `tblEmployee`
LEFT JOIN `tblMappings` ON 
`tblEmployee`.`eid` = `tblMappings`.`eid`

这将在 phpMyAdmin 中返回以下内容;

eid | name  | mid  | eid  | cid
1   | james | 1    | 1    | 2
2   | harry | NULL | NULL | NULL

我正在 PHP 中执行查询并使用:

while($row=mysql_fetch_assoc($results)) { ...blah.... }

但是对于 harry $row['eid'] 是空白的。我不知道如何解决这个问题,因此任何观点都将不胜感激。非常感谢:)

Example scenario; lets say I'm searching for employees and their employers

tblEmployee
eid | name
1  | james
2  | harry

tblCompany
cid | name
1  | ABC Ltd
2  | XYZ Corp

tblMappings
mid | eid | cid
1  | 1   | 2

James is assigned to the company XYZ Corp, but harry has no assignment. I want to pull out all employees whether they are assigned or not;

SELECT * FROM `tblEmployee`
LEFT JOIN `tblMappings` ON 
`tblEmployee`.`eid` = `tblMappings`.`eid`

This returns the following in phpMyAdmin;

eid | name  | mid  | eid  | cid
1   | james | 1    | 1    | 2
2   | harry | NULL | NULL | NULL

I'm performing the query in PHP and using:

while($row=mysql_fetch_assoc($results)) { ...blah.... }

But for harry $row['eid'] is blank. I'm not sure how to work this one out so any points would be greatly apprecited. Many thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情场扛把子 2024-12-09 19:55:45

tblEmployee 中的 eid 字段不为空,但 tblMappings 中的 eid 字段为空,这是预期的因为该表中没有匹配的记录。指定您需要哪些字段而不是SELECT * ...

SELECT e.*, m.mid, m.cid FROM `tblEmployee` e
LEFT JOIN `tblMappings` m ON 
e.`eid` = m.`eid`

The eid field from tblEmployee isn't empty, but the eid field from tblMappings is empty, which is expected since there's no matching record in that table. Specify which fields you want instead of SELECT * ....

SELECT e.*, m.mid, m.cid FROM `tblEmployee` e
LEFT JOIN `tblMappings` m ON 
e.`eid` = m.`eid`
囍孤女 2024-12-09 19:55:45
SELECT tblEmployee.* FROM `tblEmployee`
LEFT JOIN `tblMappings` ON `tblEmployee`.`eid` = `tblMappings`.`eid`

tblEmployeetblMappings 还包含列 eid
当然,tblMappings 中的第二个 eid 会覆盖第一个 eid。

为了解决这个问题,您可以在返回列中指定表名

SELECT tblEmployee.* FROM `tblEmployee`
LEFT JOIN `tblMappings` ON `tblEmployee`.`eid` = `tblMappings`.`eid`

Both tblEmployee and tblMappings also contains column eid,
and naturally the second eid from tblMappings is override the first one.

To solve this, you can specify the table name in the return columns

晨光如昨 2024-12-09 19:55:45

您不应该使用SELECT *。这是一个坏习惯。

相反,请考虑一下:

SELECT `tblEmployee`.`eid` FROM `tblEmployee`
LEFT JOIN `tblMappings` ON 
`tblEmployee`.`eid` = `tblMappings`.`eid`

You should not be using SELECT *. It's a bad habit.

Instead, consider this:

SELECT `tblEmployee`.`eid` FROM `tblEmployee`
LEFT JOIN `tblMappings` ON 
`tblEmployee`.`eid` = `tblMappings`.`eid`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文