用mysql查询3个不同的表

n3schb8v  于 2021-06-24  发布在  Mysql
关注(0)|答案(4)|浏览(214)

我有三张table:
___发票:

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

___预订:

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

___卡德克斯:

|--------|----------|-------------|-------------|
| 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之间没有任何现有的like。
因此,我的尝试如下:

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` table。
你知道为什么吗?
这里是sql小提琴:http://sqlfiddle.com/#!2011年9月1日
谢谢。
83qze16e

83qze16e1#

我不太确定你的关系或外键。但从你所付出的来看,我想这也行。可能需要稍加修改,但我想你会明白的。

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/#!2011年9月1日
如果你有任何疑问,尽管问。希望有帮助:)

dnph8jn4

dnph8jn42#

你可以试试

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'

如果您关心的是性能,那么这可能不是最佳的解决方案,但是在您的模式设计中,您不应该让一个表处理多种类型的信息,这使得编写任何合法的联接都很困难。

mum43rcc

mum43rcc3#

请检查您的第二个加入查询:

LEFT JOIN ___Kardex
ON ___Kardex.KDX_Id = ___Bookings.BOO_BillingId

应该是的

LEFT JOIN ___Kardex
ON ___Kardex.KDX_Id = ___Bookings.BOO_CompanyId

sql小提琴:http://sqlfiddle.com/#!2017年9月21日

bnlyeluc

bnlyeluc4#

您将以0的帐单ID加入\uuu 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

相关问题