简体   繁体   English

在内部联接中选择DISTINCT 2列

[英]SELECT DISTINCT 2 columns in a INNER JOIN

I'm having a problem with a query for MySQL, i have to get rows with distinct "c.email" AND distinct "pf.cliente_id", I'm doing this code below, but i'm not getting what i want, the "c.email" don't returns DISTINCT 我在查询MySQL时遇到问题,我必须获取带有不同的“ c.email”和不同的“ pf.cliente_id”的行,我在下面执行此代码,但是我没有得到我想要的, “ c.email”不返回DISTINCT

SELECT DISTINCT c.nome as nome, pf.id_cliente as cliente_id
FROM cliente c
INNER JOIN pessoa_fisica pf
ON c.id_cliente = pf.id_cliente
GROUP BY c.email, pf.id_cliente;

I Also tried: 我也尝试过:

SELECT c.nome as nome, pf.id_cliente as cliente_id
FROM cliente c
INNER JOIN pessoa_fisica pf
ON c.id_cliente = pf.id_cliente
GROUP BY c.email, pf.id_cliente;

Edit 编辑

cliente = email( It's not the primary key, is a foreign key and i have it reapeted ) cliente = email(不是主键,是外键,我已经收到了)

pessoa_fisica = id_cliente ( It's not the primary key, is a foreign key and i have it reapeted ) pessoa_fisica = id_cliente(这不是主键,是外键,我已将其改写了)

this query world work for you may be syntax error there because i have not database. 这个查询世界为您服务可能是语法错误,因为我还没有数据库。

select SUBSTRING_INDEX(full, ' ', 1) AS email ,SUBSTRING_INDEX(full, ' ', -1) AS cliente_id , nome
from 
(SELECT DISTINCT (concat(c.email as email,' ',pf.id_cliente )) as 'full' , c.nome as 'nome'
FROM cliente c
INNER JOIN pessoa_fisica pf
ON c.id_cliente = pf.id_cliente) as temp;

Mysql distinct doesn't allow multiple values, but apply little effort means first concat columns and then apply distinct then break column again. MySQL的distinct不允许有多个值,但是要花很少的精力就意味着首先使用concat列,然后再应用distinct,然后再次使用break列。 I am setting an example for you. 我为你树立一个榜样。

   Create Table: CREATE TABLE `dupli` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `fname` varchar(20) DEFAULT NULL,
      `lname` varchar(20) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

     insert into dupli (fname,lname) values ('Hitesh', 'mundra'),('Neeraj', 'sharma'),('Kailash','yadav');

     insert into dupli (fname,lname) values ('Hitesh', 'mundra'),('Neeraj', 'sharma'),('Kailash','yadav');

    select id, SUBSTRING_INDEX(name, ' ', 1) AS fname ,SUBSTRING_INDEX(name, ' ', -1) AS lname  from (select  distinct( concat(fname," ",lname) ) as Name, id from dupli) as temp;

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

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