问题
SELECT a.id,i.* FROM ads a
INNER JOIN images i ON i.ad_id=a.id
GROUP BY a.id
LIMIT 10
Can't figure out, how to pick images with flag "main" inside images table.
For one a.id can be up to 3 photos inside images table, one of those 3 photos can have a field main=1. I need to pick photos by priority where flag main is set to 1.
CREATE DATABASE IF NOT EXISTS `test123`;
USE `test123`;
CREATE TABLE IF NOT EXISTS `ads` (
`id` int(10) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=229 DEFAULT CHARSET=utf8;
INSERT INTO `ads` (`id`) VALUES (217), (225), (226), (228);
CREATE TABLE IF NOT EXISTS `images` (
`image_id` int(10) NOT NULL AUTO_INCREMENT,
`ad_id` int(10) DEFAULT '0',
`main` int(10) DEFAULT '0',
`t_0` varchar(255) DEFAULT '0',
`t_1` varchar(255) DEFAULT '0',
`t_8` varchar(255) DEFAULT '0',
PRIMARY KEY (`image_id`),
KEY `ad_id` (`ad_id`),
KEY `t_0` (`t_0`),
KEY `t_1` (`t_1`),
KEY `t_8` (`t_8`),
KEY `main` (`main`)
) ENGINE=MyISAM AUTO_INCREMENT=425 DEFAULT CHARSET=utf8;
INSERT INTO `images` (`image_id`, `ad_id`, `main`, `t_0`, `t_1`, `t_8`) VALUES
(1, 226, 0, 'img_link1', 'img_link2', 'img_link3'),
(2, 228, 0, 'img_link1', 'img_link2', 'img_link3'),
(3, 225, 0, 'img_link1', 'img_link2', 'img_link3'),
(4, 217, 0, 'img_link1', 'img_link2', 'img_link3'),
(5, 217, 1, 'img_link1', 'img_link2', 'img_link3'),
(6, 217, 0, 'img_link1', 'img_link2', 'img_link3');
Something like that but only 1 row per ads.id
SELECT a.id, i.main AS main
FROM images i
LEFT JOIN ads a ON a.id=i.ad_id
WHERE i.main=1 OR i.main=0
Output required:
id | main | links 217 | 1 | ... 225 | 0 | ... 226 | 0 | ... 228 | 0 | ...
回答1:
this should do the trick
select ad_id, main, concat(t_0, ', ', t_1, ', ', t_8) as links
from images
where main = 0
group by ad_id having ad_id not in (select ad_id from images where main = 1)
union all
select ad_id, main, concat(t_0, ', ', t_1, ', ', t_8) as links from images where main = 1
回答2:
I think this should work:
SELECT a.id,i.* FROM ads a
INNER JOIN images i
ON i.ad_id=a.id
WHERE i.main=1
GROUP BY a.id
LIMIT 10
Edit: I think you could also drop the GROUP BY clause then as there will be a 1 to 1 mapping between A and I
回答3:
You don't need a group by at all here:
SELECT ads.id,images.*
FROM ads
INNER JOIN images ON(Images.ad_id=ads.id)
WHERE Images.Flag=x
ORDER BY ads.id
LIMIT 10
If you want to include images without the flag when there is no flag, or return only 1 image per ad even if multiple images have the flag, then the query will be more difficult. You'll have to clarify to get a more targeted answer.
Edit: Something like this might work
SELECT mainImages.Ad_ID2,
IFNULL(mainImages.T_0,images.T_0) as T0,
IFNULL(mainImages.T_1,images.T_1) as T1
FROM images
inner join
(SELECT ads.id as Ad_ID2,images.*
FROM ads
LEFT OUTER JOIN images ON(Images.ad_id=ads.id)
WHERE Images.Main=1) as mainImages
on images.Ad_ID=mainImages.Ad_ID2
ORDER BY mainImages.Ad_ID2
LIMIT 10
It uses the subquery join to create a list of IDs with image data (only where the main flag is set) then re-joins the images table again and fills in the null image data from the new join if it doesn't already exist in the subquery. As I said, I can't test it here, but I think the general concept is sound.
Edit: Fixed query to use updated tables in question.
Edit: Changed column name as per comment
来源:https://stackoverflow.com/questions/3513262/mysql-problem-with-statement