[英]sql - Return set with no duplicates from inner join?
Creating a mock help desk ticketing service. 创建模拟服务台售票服务。
I have a ticket
table which holds all tickets created by the customer. 我有一个
ticket
表,其中包含客户创建的所有票证。 I have a works_on
table which holds all tickets which managers have claimed and are working on. 我有一个
works_on
表,其中包含经理要求并正在使用的所有票证。
In both tables I have a customerid
to identify the owner of the ticket. 在两个表中,我都有一个
customerid
来标识票证的所有者。
In the customer menu, they can check all of their tickets that they created, whether they are open, in the ticket
table or being worked on in the works_on
table. 在客户菜单中,他们可以检查自己创建的所有票证,无论
ticket
表中的ticket
是否已打开,是否在works_on
表中正在使用。
My code below is giving me the same ticket from both tables. 我下面的代码给了我两个表相同的凭单。 I only want to see the ticket once.
我只想看一次票。 How can I do that?
我怎样才能做到这一点?
SELECT * FROM ticket
INNER JOIN works_on
ON ticket.custid = works_on.customerid
SELECT * FROM ticket
INNER JOIN works_on
ON ticket.custid = works_on.customerid
GROUP BY ticket.custid
This will group the results where the customer ID are the same. 这将在客户ID相同的情况下对结果进行分组。
If not the modify the select into a SELECT DISTINCT 如果不是,则将选择修改为SELECT DISTINCT
You're joining on the wrong columns. 您加入了错误的专栏。 When you join on
customerid
, it finds all the tickets for a customer, and all the works_on
rows for that customer, and creates a cross-product between them. 当您加入
customerid
,它将找到该customerid
所有票证以及该客户的所有works_on
行,并在它们之间创建叉积。 You should be joining on the ticket ID, so you get each ticket with its corresponding works_on
row. 您应该加入票证ID,以便获得每张票证及其对应的
works_on
行。
SELECT *
FROM ticket AS t
INNER JOIN works_on AS w ON t.ticket_id = w.ticket_id
WHERE t.custid = :customer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.