我正在尝试创建一个查询,在该查询中我选择了多个表以及每个表的一些数据,但是如果我创建count
,它将失败。我希望有人能告诉我我在做什么错。
这是可以正常工作并带给我所有数据的人:
SELECT *
FROM profiles p
LEFT OUTER JOIN(
SELECT u.id
, u.nickname
, u.email
, w.idUserWished
, l.idUserLiked
, b.idBlocked
, m.idMatched
, m.accepted
FROM users u
LEFT OUTER JOIN wish w ON w.idUserWished = u.id
LEFT OUTER JOIN likes l ON l.idUserLiked = u.id
LEFT OUTER JOIN blocked b ON b.idBlocked = u.id
LEFT OUTER JOIN matches m ON m.idMatched = u.id
WHERE NOT EXISTS (
SELECT idBlock
FROM blocked b
WHERE b.idBlock = u.id AND b.idBlocked = 1)
AND u.id <> 1
ORDER BY id DESC LIMIT 0,5) AS p2
ON p.idUser = p2.id
WHERE p.gender = 'Femenino'
AND p.age BETWEEN 18 AND 24
AND p.alcohol = 'A diario'
AND p.smoke = 'A diario'
AND p.antro = 'A diario'
AND p.edoCivil = 'Casado'
AND p.height = 100
AND p.weight = 'Delgado'
AND p.skin = 'Clara'
AND p.location = 'Mexico'
AND p.state = 'Coahuila'
但是,如果我包含count
,它将失败,仅带给我1条记录
SELECT *
FROM profiles p
LEFT OUTER JOIN(
SELECT u.id
, u.nickname
, u.email
, w.idUserWished
, l.idUserLiked
, b.idBlocked
, m.idMatched
, m.accepted
, COUNT(DISTINCT msn.UserReceiver) AS 'cantidad'
FROM users u
LEFT OUTER JOIN wish w ON w.idUserWished = u.id
LEFT OUTER JOIN likes l ON l.idUserLiked = u.id
LEFT OUTER JOIN blocked b ON b.idBlocked = u.id
LEFT OUTER JOIN matches m ON m.idMatched = u.id
LEFT OUTER JOIN messages msn ON msn.userEmitter = 1
WHERE NOT EXISTS (
SELECT idBlock
FROM blocked b
WHERE b.idBlock = u.id AND b.idBlocked = 1)
AND u.id <> 1 ORDER BY id DESC LIMIT 0,5) AS p2
ON p.idUser = p2.id
WHERE p.gender = 'Femenino'
AND p.age BETWEEN 18 AND 24
AND p.alcohol = 'A diario'
AND p.smoke = 'A diario'
AND p.antro = 'A diario'
AND p.edoCivil = 'Casado'
AND p.height = 100
AND p.weight = 'Delgado'
AND p.skin = 'Clara'
AND p.location = 'Mexico'
AND p.state = 'Coahuila'
下一个查询示例
如果您能看到区别...当我设置计数时,只需给我带来1个昵称和1个电子邮件...但我希望给我带来所有数据
答案 0 :(得分:1)
这里的问题是您使用的是count
,而没有group by
子句。在另一个数据库引擎中,它会导致语法错误,但在mysql中,它仍然可以接受并正常工作。但是,让我们看看mysql是如何通过小测试来工作的
SELECT *, COUNT(*) AS 'cantidad' FROM users
此查询仅返回一个记录,即使表用户有很多记录。记录返回是表用户中第一条记录的总和,并对所有用户计数。
因此,不要在没有count
的情况下使用group by
,这会使查询不清楚。即使mysql仍然可以使用它,也很难弄清楚我的sql如何执行查询
要解决您的问题,因为您没有提供示例数据并无法预期结果,因此我不确定如何确切地解决您的问题。但是根据您的查询逻辑,我建议您更改
, COUNT(DISTINCT msn.UserReceiver) AS 'cantidad'
....
LEFT OUTER JOIN messages msn ON msn.userEmitter = 1
通过子查询
,(select COUNT(DISTINCT msn.UserReceiver) from messages msn where msn.userEmitter = 1) AS 'cantidad'
所以您的查询会这样
SELECT *
FROM profiles p
LEFT OUTER JOIN(
SELECT u.id
, u.nickname
, u.email
, w.idUserWished
, l.idUserLiked
, b.idBlocked
, m.idMatched
, m.accepted
,(select COUNT(DISTINCT msn.UserReceiver) from messages msn where msn.userEmitter = 1) AS 'cantidad'
FROM users u
LEFT OUTER JOIN wish w ON w.idUserWished = u.id
LEFT OUTER JOIN likes l ON l.idUserLiked = u.id
LEFT OUTER JOIN blocked b ON b.idBlocked = u.id
LEFT OUTER JOIN matches m ON m.idMatched = u.id
WHERE NOT EXISTS (
SELECT idBlock
FROM blocked b
WHERE b.idBlock = u.id AND b.idBlocked = 1)
AND u.id <> 1
ORDER BY id DESC LIMIT 0,5) AS p2
ON p.idUser = p2.id
WHERE p.gender = 'Femenino'
AND p.age BETWEEN 18 AND 24
AND p.alcohol = 'A diario'
AND p.smoke = 'A diario'
AND p.antro = 'A diario'
AND p.edoCivil = 'Casado'
AND p.height = 100
AND p.weight = 'Delgado'
AND p.skin = 'Clara'
AND p.location = 'Mexico'
AND p.state = 'Coahuila'