我正在尝试从个人资料相册中获取用户ID,fname,lname,图片,封面值为。
我正在使用以下查询
select distinct
a.id as id,
a.fname as fname,
a.lname as lname,
im.sm_pic_path as image,
im.is_album_cover as cover,
album.name as album
from
user a
left outer join
images im
on a.id=im.aid
left outer join
album album
on im.album_id=album.id
where
a.fname like '%man%'
or a.lname like '%man%'
and a.status='active'
and im.is_album_cover='Yes'
and album.name='Profile'
group by a.id
order by
im.sm_pic_path desc,a.id
limit 0,30
此查询正常运行,但无法获取正确的图像。 我想要的图像的专辑名称是个人资料,而is_album_cover值是肯定的。
如何使其正确。
答案 0 :(得分:4)
对我来说,您的查询中有几个错误:
INNER JOIN
,因为您已在WHERE
条款中添加了以下条件:and im.is_album_cover='Yes'
WHERE
条件中,您必须使用括号GROUP BY
子句,否则MySql引擎占用第一行。所以对我来说,你的查询可以用这种方式重写:
SELECT
a.id as id,
a.fname as fname,
a.lname as lname,
(SELECT im.sm_pic_path
FROM images im
WHERE a.id=im.aid
AND im.is_album_cover='Yes') as image,
(SELECT im.is_album_cover
FROM images im
WHERE a.id=im.aid
AND im.is_album_cover='Yes') AS cover,
(SELECT album.name
FROM images im
JOIN album
ON im.album_id=album.id
WHERE a.id = im.aid
AND album.name='Profile'
AND im.is_album_cover='Yes') AS album_name
FROM user a
WHERE (a.fname LIKE '%man%' OR a.lname like '%man%')
AND a.status = 'active'
ORDER BY
image desc,a.id
LIMIT 0,30