INNER JOIN中的SELECT DISTINCT 2列

时间:2015-04-08 19:56:07

标签: mysql sql

我对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;

我也试过了:

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;

修改

cliente =电子邮件(它不是主键,是外键,我让它重新开始)

pessoa_fisica = id_cliente(它不是主键,是外键,我让它重新开始)

1 个答案:

答案 0 :(得分:0)

这个查询世界为你工作可能是语法错误,因为我没有数据库。

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不允许多个值,但应用很少的工作意味着第一个concat列,然后再次应用distinct而break列。我正在为你树立榜样。

   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;