简体   繁体   English

Mysql查询创建一个带有join和distinct的视图

[英]Mysql query create a view with join and distinct

i need some helps with a query that i can create correctly for what i want. 我需要一些查询,可以为自己想要的内容正确创建查询。

i'm explaining all: 我正在解释所有:

accounts ( id, name, surname,email, telephone ..) with id pk boc (ad,username, port..) with ad pk . ID为pk boc的帐户(ID,名称,姓氏,电子邮件,电话..)以及广告为pk的帐户(广告,用户名,端口..)。

Telephone and Username have the same values in their tables 电话和用户名在表中具有相同的值

i need to create a view that's report all fields are into table accounts and table bow and that have as ports 16 and 17. 我需要创建一个视图,该视图报告所有字段都已进入表帐户和表弓,并且具有端口16和17。

My query is: 我的查询是:

CREATE VIEW exportable 
AS
(SELECT A.ID,A.NOME,A.SURNAME,A.EMAIL,A.TELEPHONE,B.AD,B.USERNAME,B.PORT
  FROM ACCOUNTS A, BOC B
 WHERE A.TELEPHONE = B.USERNAME AND B.PORT= 16 AND 17);

this query works in a good way, i have all field and row are completed, but obv this kind of query doesn't exclude some copies row, that i need to exclude. 该查询以一种很好的方式工作,我已经完成了所有字段和行,但是,这种查询不会排除某些需要复制的行。 Can someone give me a suggestion ? 有人可以给我一个建议吗?

Thank you. 谢谢。 Regards 问候

first think i notice is that you you should use JOIN instead of this all fashion way of joining table... Your query may look like this than 首先认为我注意到您应该使用JOIN而不是这种连接表的方式...您的查询可能比

SELECT A.ID,A.NOME,A.SURNAME,A.EMAIL,A.TELEPHONE,B.AD,B.USERNAME,B.PORT
FROM ACCOUNTS A
INNER JOIN BOC B
ON A.TELEPHONE = B.USERNAME 
WHERE B.PORT = 16 OR B.PORT = 17

I understand that B.PORT should be 16 OR 17 I'm not quiet sure how your table data looks like... 我了解B.PORT应该为16或17。我不确定您的表数据是什么样子...

GL! GL!

Just add this to your "where" statement to exclude some rows: 只需将其添加到“ where”语句中即可排除一些行:

and A.id not in (12, 13, 14)

where 12, 13 and 14 are the exception id's. 其中12、13和14是例外ID。

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

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