使用MySQL查询3个不同的表

时间:2018-07-07 03:42:30

标签: mysql sql database

我有这3张桌子:

___发票:

|--------|---------------|
| INV_Id | IVC_BookingId |
|--------|---------------|
| 10     | 31            |
|--------|---------------|

___预定:

|--------|-------------|---------------|---------------|
| BOO_Id | BOO_GuestId | BOO_CompanyId | BOO_BillingId |
|--------|-------------|---------------|---------------|
| 10     | 89            90            | 0             |
|--------|-------------|---------------|---------------|

___ Kardex:

|--------|----------|-------------|-------------|
| KDX_Id | KDX_Type | KDX_Name    | KDX_Company |
|--------|----------|-------------|-------------|
| 89     | guest    | Frank       |             |
| 90     | company  |             | Google      |
|--------|----------|-------------|-------------|

我想为发票找到链接的卡用户。

例如,对于INV_Id = 10,它应该返回我:

|--------|-------|---------|---------|
| INV_Id | guest | company | billing |
|--------|-------|---------|---------|
| 10     | Frank | Google  | 0       |
|--------|-------|---------|---------|

billing实际上是空的,因为我的发票与预订和kardex之间没有任何类似的东西。

所以我的尝试如下:

SELECT IVC_Id, IVC_BookingId, IFNULL(BOO_GuestId, 0) AS BOO_GuestId, IFNULL(BOO_CompanyId, 0) AS BOO_CompanyId, IFNULL(BOO_BillingId, 0) AS BOO_BillingId, KDX_Name, KDX_Company 
FROM ___Invoices 
JOIN ___Bookings
ON ___Bookings.BOO_Id = ___Invoices.IVC_BookingId
LEFT JOIN ___Kardex
ON ___Kardex.KDX_Id = ___Bookings.BOO_BillingId
WHERE IVC_Id='10'

我没有错误,但是我可以从___Kardex表中获取名称或公司。

你知道为什么吗?

这里是SQL提琴:http://sqlfiddle.com/#!9/211d01/1

谢谢。

4 个答案:

答案 0 :(得分:1)

对于您的关系或外键,我不太确定。但是,根据您提供的信息,我认为这可以做到。可能需要稍作修改,但我想您会明白的。

SELECT ___Invoices.IVC_Id, k1.KDX_Name as guest, k2.KDX_Company as company
FROM ___Invoices 
JOIN ___Bookings
ON ___Bookings.BOO_Id = ___Invoices.IVC_BookingId
LEFT JOIN ___Kardex k1 ON ___Bookings.BOO_GuestId = k1.KDX_Id
LEFT JOIN ___Kardex k2 ON ___Bookings.BOO_CompanyId = k2.KDX_Id
WHERE ___Invoices.IVC_Id='10'

请检查此内容,并让我知道是否是您想要的。

http://sqlfiddle.com/#!9/211d01/31

随时询问您是否有任何疑问。希望对您有所帮助:)

答案 1 :(得分:0)

您正以BillingID为0加入__Kardex。

0不等于89或90

Your code


LEFT JOIN ___Kardex
ON ___Kardex.KDX_Id = ___Bookings.BOO_BillingId

这是我的解决方法:我两次以不同的联接条件联接到同一张表。然后为该联接创建一个新别名:

CREATE TABLE `inv` (`INV_ID` VARCHAR(255) NOT NULL, `IVC_BookingID` 
VARCHAR(255) NOT NULL);

CREATE TABLE `bookings` (`BOO_id` VARCHAR(255) NOT NULL, `BOO_guestid` VARCHAR(255) NOT NULL, `BOO_CompanyID` VARCHAR(255) NOT NULL, `BOO_BillingID` VARCHAR(255) NOT NULL);

CREATE TABLE `kardex` (`KDX_ID` VARCHAR(255) NOT NULL, `KDX_TYPE` VARCHAR(255) NOT NULL, `KDX_NAME` VARCHAR(255) NOT NULL, `KDX_COMPANY` VARCHAR(255) NOT NULL);

INSERT INTO `inv` (INV_ID,IVC_BookingID) SELECT '10','31';
INSERT INTO `bookings` (BOO_id,BOO_guestid,BOO_CompanyID,BOO_BillingID) SELECT '10','89','90','0';
INSERT INTO `kardex` (KDX_ID,KDX_TYPE,KDX_NAME,KDX_COMPANY) SELECT '89','guest','Frank','';
INSERT INTO `kardex` (KDX_ID,KDX_TYPE,KDX_NAME,KDX_COMPANY) SELECT '90','company','','Google';

SELECT a.INV_ID, c.kdx_name, d.kdx_company FROM inv a 
INNER JOIN bookings b ON b.boo_id=a.INV_ID
INNER JOIN kardex c ON c.kdx_id=b.boo_guestid
INNER JOIN kardex d ON d.kdx_id=b.BOO_CompanyID
WHERE INV_ID='10';

结果

INV_ID  kdx_name    kdx_company
10       Frank      Google

答案 2 :(得分:0)

请检查您的第二个JOIN查询:

LEFT JOIN ___Kardex
ON ___Kardex.KDX_Id = ___Bookings.BOO_BillingId

应该是

LEFT JOIN ___Kardex
ON ___Kardex.KDX_Id = ___Bookings.BOO_CompanyId

SQL小提琴:http://sqlfiddle.com/#!9/211d01/7

答案 3 :(得分:0)

您可以尝试

SELECT IVC_Id, 
(SELECT KDX_Name FROM ___Kardex WHERE KDX_Id= ___Bookings.BOO_GuestId) AS guest,
(SELECT KDX_Company FROM ___Kardex WHERE KDX_Id= ___Bookings.BOO_CompanyId) AS company,
___Bookings.BOO_BillingId as billing
FROM ___Invoices 
JOIN ___Bookings
ON ___Bookings.BOO_Id = ___Invoices.IVC_BookingId
WHERE IVC_Id='10'

如果您关注的是性能,它可能不是最佳的解决方案,但是在架构设计中,您不应让一个表处理多于一种类型的信息,因此很难编写任何合法的联接。