简体   繁体   English

mysql完全连接查询

[英]mysql full join query

What's wrong with the following query. 以下查询出了什么问题。 It's not working and I basically need it to do a full join and return all fields whether there is a match or not. 它不起作用,我基本上需要它来进行完全连接并返回所有字段,无论是否匹配。

SELECT tbl_tickets.ID AS Ticket_ID, tbl_tickets.Ticket_Number, tbl_tickets.Ticket_Category, tbl_orders.PO_Number, tbl_invoice_to.Name AS Dealer_Name 
FROM tbl_tickets 
FULL INNER JOIN tbl_orders ON tbl_tickets.Order_ID=tbl_orders.ID 
FULL INNER JOIN tbl_invoice_to ON tbl_tickets.Invoice_To=tbl_invoice_to.ID
WHERE Ticket_Type='PARENT' AND {$where} 
ORDER BY tbl_tickets.Ticket_Category

AFAIK, there's no such thing as a FULL INNER JOIN . AFAIK,没有FULL INNER JOIN之类的东西。 It's probably falling back to a regular INNER JOIN or doing something else entirely. 它可能会退回到常规的INNER JOIN或完全执行其他操作。

I think you're looking for an OUTER -type join. 我认为您正在寻找OUTER型联接。 I would start with changing these to LEFT JOIN to see if that returns the results you're looking for. 我将从将它们更改为LEFT JOIN ,看看是否返回您想要的结果。

If it does not, and you truly do need a FULL OUTER JOIN , then see Full Outer Join in MySQL for an example on how to achieve one in MySQL. 如果不是这样,而您确实确实需要FULL OUTER JOIN ,那么请参阅MySQL中的Full Outer Join,以获取有关如何在MySQL中实现一个的示例。

Given the names of your tables, I would expect the foreign key relationships to line up. 给定表的名称,我希望外键关系排成一行。 Hence, an inner join should be sufficient: 因此, inner join应该足够了:

SELECT t.ID AS Ticket_ID, t.Ticket_Number, t.Ticket_Category, o.PO_Number, i.Name AS Dealer_Name 
FROM tbl_tickets t INNER JOIN
     tbl_orders o
     ON t.Order_ID = o.ID INNER JOIN
     tbl_invoice_to i
     ON t.Invoice_To = i.ID
WHERE Ticket_Type = 'PARENT' AND {$where} 
ORDER BY t.Ticket_Category

(Note I introduced table aliases, which make the query easier to write and to read.) (请注意,我引入了表别名,这使查询更易于编写和阅读。)

If this is returning nothing, then start by removing conditions in the WHERE clause. 如果没有返回任何内容,则从删除WHERE子句中的条件开始。 If still nothing is returned, then check the join condition. 如果仍然没有返回任何内容,则检查连接条件。

MySQL does not support full outer join. MySQL不支持完全外部联接。 So, when you write: 因此,当您编写:

FROM tbl_tickets FULL INNTER JOIN
     tbl_orders . . .

You are assigning the alias FULL to tbl_tickets -- definitely not your intention. 您正在将别名FULL分配给tbl_tickets绝对不是您的意图。

You need to do a left join it returns all the records of the first table whether there match or no . 您需要执行左连接,它返回第一个表的所有记录,无论是否匹配。 and if you want to do a full outer join yo need to do a left join - union - right join because there's not full outer join in MySQL 如果要进行完全外部联接,则需要执行左联接-联合-右联接,因为MySQL中没有完全外部联接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM