Why operator LIMIT does not work correctly? - mysql

I wrote the next query SQL for getting last comments with re-comments:
SELECT c.*, ar.ArticleName, ar.idArticle, du.DetailToUsersName, du.DetailToUsersPhoto, COUNT(c.idCommentToArticle) AS CNT, CASE WHEN d.Count IS NULL THEN 0 ELSE d.Count END AS CountLikes
FROM commenttoarticle c
INNER JOIN (SELECT CommentToArticlePID FROM commenttoarticle
GROUP BY CommentToArticlePID
ORDER BY CommentToArticlePID LIMIT 3) AS articleComments
USING (CommentToArticlePID)
LEFT JOIN article ar ON c.CommentToArticleIdArticle = ar.idArticle
LEFT JOIN detailtousers du ON du.idDetailToUsers = c.CommentToArticleIdUser
LEFT JOIN `likes` d ON (d.IdNote = c.idCommentToArticle AND d.LikeType = 6)
WHERE c.CommentToArticleIdArticle = 11
GROUP BY c.idCommentToArticle
So, why operator LIMIT 3 in sub-query select does not work? Now this query shows all rows from table commenttoarticle
I seem that need do somethink like as:
SELECT...
FROM (select * from commenttoarticle commenttoarticle c INNER JOIN
(SELECT distinct(CommentToArticlePID)
FROM commenttoarticle b
ORDER BY CommentToArticlePID
LIMIT 2) AS commenttoarticle USING (CommentToArticlePID)) as c
LEFT JOIN article ar ON c.CommentToArticleIdArticle = ar.idArticle...
Dump table commenttoarticle:
CREATE TABLE IF NOT EXISTS `commenttoarticle` (
`idCommentToArticle` int(11) NOT NULL AUTO_INCREMENT,
`CommentToArticleTime` int(11) NOT NULL,
`CommentToArticleIdArticle` int(11) NOT NULL,
`CommentToArticleComment` text NOT NULL,
`CommentToArticleIdUser` int(11) NOT NULL,
`CommentToArticlePID` int(11) NOT NULL,
PRIMARY KEY (`idCommentToArticle`),
UNIQUE KEY `idCommentToArticle_UNIQUE` (`idCommentToArticle`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=59 ;
INSERT INTO `commenttoarticle` (`idCommentToArticle`, `CommentToArticleTime`, `CommentToArticleIdArticle`, `CommentToArticleComment`, `CommentToArticleIdUser`, `CommentToArticlePID`) VALUES
(29, 0, 11, 'продажам?\nИнтересует не мега-звезда, а именно предметный, руками умеющий продавать сам и помогающий выстраивать это бизнесам.', 459, 0),
(30, 0, 11, '2', 459, 0),
(31, 0, 11, '3', 459, 0),
(36, 0, 11, '3.1', 459, 31),
(37, 1413822798, 11, 'also facing that prob. on the plteform of win 7', 459, 29),
(38, 0, 11, ' here i dont have internet connection.. #Samint Sinha thanks ill check it out maybe tomorrow.', 459, 29),
(39, 0, 11, ' Select max id and you will have dhe last row returned', 459, 29),
(32, 0, 11, '4', 459, 0),
(44, 1414354324, 11, 'How to do', 456, 29),
(45, 1414354469, 11, 'sfsfsf', 456, 29),
(46, 1414354708, 11, 'dddd', 456, 29),
(47, 1414357761, 11, 'sfsfs', 456, 0),
(57, 1414370833, 39, 'kkkppppppp', 456, 0),
(49, 1414358233, 11, 'VSF\nSFSF', 456, 0),
(50, 1414359589, 11, 'How to do', 456, 0),
(51, 1414359660, 11, 'sfsfsdf', 456, 0),
(52, 1414361057, 11, 'SDFSF', 456, 0),
(53, 1414364023, 11, 'dsfdsjfsifmsi', 456, 0),
(54, 1414364031, 11, 'sdfdskjfnskf', 456, 52),
(55, 1414364034, 11, 'sdfdskjfnskf', 456, 52),
(56, 1414364044, 11, 'fndsdfnsofosfi', 456, 52),
(58, 1414370841, 39, 'dfgdfgdgdgdgdgdfgdgdfg', 456, 0);
Dump table article:
CREATE TABLE IF NOT EXISTS `article` (
`idArticle` int(11) NOT NULL AUTO_INCREMENT,
`ArticleName` varchar(255) NOT NULL,
`ArticleTime` int(11) NOT NULL,
`ArticleDescription` varchar(500) NOT NULL,
`ArticleText` text NOT NULL,
`ArticleToUserID` int(11) DEFAULT NULL,
`ArticleCategory` int(11) NOT NULL,
`ArticleView` int(11) NOT NULL,
`ArticleCountry` int(11) NOT NULL,
`ArticlePhoto` varchar(150) NOT NULL,
`ArticleCity` int(11) NOT NULL,
PRIMARY KEY (`idArticle`),
UNIQUE KEY `idArticle_UNIQUE` (`idArticle`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=50 ;

By constructing a self join on comments yuo're not limiting but multiply the result.
What you need is a single subquery on comments table
SELECT c.*, ar.ArticleName,
ar.idArticle, du.DetailToUsersName,
du.DetailToUsersPhoto, COALECSE(SUM(d.count), 0)
FROM
(SELECT *
FROM
commenttoarticle
WHERE
CommentToArticleIdArticle = 11
ORDER BY
CommentToArticlePID, idCommentToArticle DESC
LIMIT 3
) c
LEFT JOIN
article ar
ON c.CommentToArticleIdArticle = ar.idArticle
LEFT JOIN
detailtousers du
ON du.idDetailToUsers = c.CommentToArticleIdUser
LEFT JOIN
likes d
ON (d.IdNote = c.idCommentToArticle AND d.LikeType = 6)
GROUP BY
c.idCommentToArticle
I have made the assumption that a comment can have multiple entries in the likes table, otherwise the subquery and the group by is not necesarry.

The USING(column_list) clause names a list of columns that must exist in both tables.
reference: http://dev.mysql.com/doc/refman/5.0/en/join.html
CommentToArticlePID is not in your first table.
Also, what is the point of joining a single column? Yes, if you want to limit, this might be applicable, but then why us LIMIT?

Related

after left-join result, need a countresult per user

I have managed to create a query, which even works.
SELECT voterID,vote,nick as player
FROM
trust LEFT JOIN players
ON trust.playerID=players.playerID
ORDER BY trust.vote DESC, trust.playerID DESC
This gives me a full list of all votes, but I want to SUM the votes per player, so i get 1 row per player, with 1 total amount of trustpoints.(can be positive or negative. +1 or -1 per voted, per voter)
The table trust:
CREATE TABLE `trust` (
`rowID` int(10) UNSIGNED NOT NULL,
`playerID` int(11) UNSIGNED NOT NULL,
`voterID` int(11) UNSIGNED NOT NULL,
`vote` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `trust` (`rowID`, `playerID`, `voterID`, `vote`) VALUES
(25, 2187, 1, 1),
(26, 23193, 1, 1),
(27, 2050, 1, 1),
(29, 3714, 1, 1),
(31, 1, 2187, 1),
(32, 30363, 29937, 1),
(33, 15837, 26102, 1),
(34, 30058, 26102, 1),
(35, 30539, 26102, -1),
(36, 28382, 26102, -1),
(37, 18692, 26102, 1),
(38, 6440, 14143, 1),
(39, 15069, 8306, 1),
(40, 2050, 2187, 1),
(41, 3233, 1, 1),
(42, 12664, 26102, 1),
(43, 30539, 2187, -1),
(44, 28382, 2187, -1),
(45, 30539, 1, -1),
(46, 10138, 1, 1);
Expecting result: a list of names and total SUM of votes
You can join the player table on an aggregate query of the votes:
SELECT nick AS player, sum_votes
FROM players p
JOIN (SELECT playerid, SUM(vote) AS sum_votes
FROM trust
GROUP BY playerid) t ON p.playerid = t.playerid
ORDER BY 2 DESC

MySQL - Query not returning proper results

CREATE TABLE `swipes` (
`swp_id` bigint(20) NOT NULL,
`swp_by` bigint(20) NOT NULL,
`swp_to` bigint(20) NOT NULL,
`swp_type` varchar(255) NOT NULL,
`swp_status` enum('requested','accepted','declined') NOT NULL,
`swp_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `swipes` (`swp_id`, `swp_by`, `swp_to`, `swp_type`, `swp_status`, `swp_date`) VALUES
(1, 8, 11, 'top', 'accepted', '2020-04-18 20:48:45'),
(2, 1, 11, 'right', 'accepted', '2020-04-18 20:41:49'),
(3, 12, 1, 'right', 'accepted', '2020-04-18 20:41:49'),
(4, 13, 1, 'right', 'accepted', '2020-04-18 20:41:49'),
(5, 1, 14, 'right', 'accepted', '2020-04-18 20:41:49'),
(6, 1, 15, 'top', 'accepted', '2020-04-18 20:41:49');
CREATE TABLE `messages` (
`msg_id` bigint(20) NOT NULL,
`msg_from` bigint(20) NOT NULL,
`msg_to` bigint(20) NOT NULL,
`msg_message` longtext NOT NULL,
`msg_seen` enum('yes','no') NOT NULL DEFAULT 'no',
`msg_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `messages` (`msg_id`, `msg_from`, `msg_to`, `msg_message`, `msg_seen`, `msg_time`) VALUES
(1, 11, 1, 'How are you?', 'yes', '2020-04-14 21:01:05'),
(3, 1, 11, 'I am fine.. you?', 'no', '2020-04-14 20:54:07'),
(4, 1, 8, 'How are you?', 'yes', '2020-04-14 21:01:05'),
(5, 8, 1, 'I am good... You say?', 'yes', '2020-04-14 21:13:34'),
(6, 1, 11, 'Thik hun... Tum batao..', 'yes', '2020-04-14 21:16:05'),
(7, 11, 1, 'Okay', 'yes', '2020-04-16 09:16:39'),
(8, 8, 1, 'Yes, it\'s a good idea.', 'yes', '2020-04-16 09:16:39'),
(9, 1, 8, 'Thought so.. Would you like to join?', 'yes', '2020-04-16 09:23:39'),
(10, 8, 1, 'Are you there?', 'yes', '2020-04-23 11:57:39'),
(12, 8, 1, 'Would you like to join?', 'yes', '2020-04-23 10:42:27'),
(13, 1, 11, 'We will arrange things for you :)', 'yes', '2020-04-23 10:59:04');
Fiddle: DB FIDDLE
In the fiddle above my query is returning correct data but it seems to eliminate results when 1 is present in multiple records of swp_by in swipes table. At first I thought it was because of GROUP BY swipes.swp_by but I removed it and it seemed like it wasn't the issue so I placed it back. When you run the query you see that currently the query returns result for swp_id 2, 3 & 4 but not for 5 & 6. They are eliminated and it's because in swp_by 1 occurred in swp_id 2 once. I want the query to return the results for 5 & 6 as well.
Query
SELECT
swp_id,
swp_by,
swp_to,
msg_from,
msg_to,
msg_message,
GREATEST(MAX(msg_time), swipes.swp_date) AS msgdate,
COUNT(msg_id) AS msgcnt
FROM
swipes
LEFT JOIN
(SELECT
*
FROM
messages
ORDER BY msg_time DESC) messages ON ((messages.msg_from = swipes.swp_by
AND messages.msg_to = swipes.swp_to)
OR (messages.msg_from = swipes.swp_to
AND messages.msg_to = swipes.swp_by))
WHERE
(swipes.swp_by = 1 OR swipes.swp_to = 1)
AND swipes.swp_status = 'accepted'
GROUP BY swipes.swp_by
ORDER BY GREATEST(MAX(messages.msg_time),
MAX(swipes.swp_date)) DESC
What is this all about?
I am setting up a chatting system and users who are matched are able chat with each other. swipes stores the matches and messages are the transacted messages between the users. With this query I am trying to set up the home list of matched users to chat with where you tap/click a user and the chat box appears (will make that later). I thought just giving a brief about the project might help in understanding the problem.
I post my asnwer here the fiddle doesn't give the right link
no got it https://www.db-fiddle.com/f/2yKt6d5RWngXVYJKPGZL6m/2
SELECT
swp_id,
swp_by,
swp_to,
msg_from,
msg_to,
msg_message ,
GREATEST(MAX(msg_time), swipes.swp_date) AS msgdate,
COUNT(msg_id) AS msgcnt
FROM
swipes
LEFT JOIN
(SELECT
*
FROM
messages
ORDER BY msg_time DESC) messages
ON (messages.msg_from = swipes.swp_by
AND messages.msg_to = swipes.swp_to)
OR
(messages.msg_from = swipes.swp_to
AND messages.msg_to = swipes.swp_by)
WHERE
(swipes.swp_by = 1 OR swipes.swp_to = 1)
AND swipes.swp_status = 'accepted'
GROUP BY swipes.swp_by,swipes.swp_to
ORDER BY GREATEST(MAX(messages.msg_time),
MAX(swipes.swp_date)) DESC

SELECT query for TableA and TableB IF TableB.col value (0 OR 1) I create new columns for 0 and 1

I have a Table pictures
create table pictures (
picture_id int(10) unsigned NOT NULL AUTO_INCREMENT,
mall_id float NOT NULL,
shop_id float NOT NULL,
picture_islogo int(11) NOT NULL ,
picture_path varchar(255) NOT NULL,
PRIMARY KEY (picture_id)
) ;
Sample Data for pictures
INSERT INTO pictures VALUES
(1, -1, 1, 1, 'photo.jpg'),
(2, -1, 2, 1, 'photo.jpg'),
(3, -1, 3, 1, 'photo.jpg'),
(4, -1, 4, 1, 'photo.jpg'),
(5, -1, 5, 1, 'photo.jpg'),
(6, -1, 6, 1, 'photo.jpg'),
(7, -1, 7, 1, 'photo.jpg'),
(8, -1, 8, 1, 'photo.jpg'),
(9, -1, 9, 1, 'photo.jpg'),
(10, -1, 10, 1, 'photo.jpg'),
(11, -1, 11, 1, 'photo.jpg'),
(12, -1, 12, 1, 'photo.jpg'),
(13, -1, 13, 1, 'photo.jpg'),
(14, -1, 13, 0, 'photo.jpg');
and other table malls
create table malls (
mall_id float NOT NULL AUTO_INCREMENT,
user_id float NOT NULL,
mall_displaysname varchar(255) NOT NULL,
mall_description text NOT NULL,
mall_contact varchar(14) NOT NULL,
mall_logo_picture_id int(11) NOT NULL,
mall_background_picture_id int(11) NOT NULL,
PRIMARY KEY (mall_id)
);
Sample Data for malls
INSERT INTO malls VALUES
(1, 2, 'mall', 'description', '+60 12 3456789', 14, 36),
(2, 5, 'mall 2', 'description', '+60 12 3456789', 15, 37),
(3, 6, 'mall 3', 'description ', '+60 12 3456789', 16, 38),
(4, 13, 'Multimedia University', 'description ', '+60 12 3456789', 17, 39),
(5, 18, 'Setia Walk', 'description ', '+60 12 3456789', 18, 40),
(6, 20, 'Ampang Point', 'description ', '+60 12 3456789', 19, 41),
(7, 21, 'Alamanda Plaza', 'description ', '+60 12 3456789', 20, 42),
(8, 22, 'Subang Parade', 'description ', '+60 12 3456789', 21, 43),
(9, 26, 'مجمع العرب - جدة', 'description ', '+60 12 3456789', 22, 44);
Where mall_background_picture_id and mall_background_picture_id reference to pictures.picture_id and Depend on the value of pictures.picture_islogo (0: Logo, 1:background or others)
i want a query to that returns the pictures.picture_path depends on the value of pictures.picture_islogo (0: Logo, 1:background or others)
the data that I need to get
mall_id , mall_displaysname, mall_logo, mall_background , mall_description, and mall_contact
Where mall_logo is the photo_path of the condition
pictures.picture_islogo = 1
and same thing with **mall_background** but
picture_islogo = 0
AND
mall_logo_picture_id = picture_id
Put the extra condition into the join condition:
select * -- you can chose what columns you want
from malls m
left join pictures pl on m.mall_logo_picture_id = pl.picture_id
and pl.picture_isLogo = 1
left join pictures pb on m.mall_background_picture_id = pb.picture_id
and pb.picture_isLogo = 0
To have left joins, the extra conditions must be in the join condition, which is evaluated as the join is being made.
If you put them in the where clause, which is evaluated after the join is made, you force the outer join to be an inner join, because the join must be successful fur the extra condition to be true.

Return all rows from sub-query SQL?

Now I wrote query SQL for getting rows limited by first query:
SELECT * FROM commenttoarticle a
WHERE a.idCommentToArticle = (SELECT CommentToArticlePID FROM commenttoarticle b)
ORDER BY a.idCommentToArticle DESC LIMIT 3
When I try to execute this query, I get:
#1242 - Subquery returns more than 1 row
How to resolve this issue? So, I need get all rows from sub-query.
If I want return one row - I need use GROUP BY, but it is not solution
Modified query:
SELECT a.idCommentToArticle FROM
commenttoarticle a WHERE a.CommentToArticlePID IN
(SELECT idCommentToArticle FROM commenttoarticle b) ORDER BY a.idCommentToArticle DESC LIMIT 3
Dump table commenttoarticle:
CREATE TABLE IF NOT EXISTS `commenttoarticle` (
`idCommentToArticle` int(11) NOT NULL AUTO_INCREMENT,
`CommentToArticleTime` int(11) NOT NULL,
`CommentToArticleIdArticle` int(11) NOT NULL,
`CommentToArticleComment` text NOT NULL,
`CommentToArticleIdUser` int(11) NOT NULL,
`CommentToArticlePID` int(11) NOT NULL,
PRIMARY KEY (`idCommentToArticle`),
UNIQUE KEY `idCommentToArticle_UNIQUE` (`idCommentToArticle`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=59 ;
--
-- Дамп данных таблицы `commenttoarticle`
--
INSERT INTO `commenttoarticle` (`idCommentToArticle`, `CommentToArticleTime`, `CommentToArticleIdArticle`, `CommentToArticleComment`, `CommentToArticleIdUser`, `CommentToArticlePID`) VALUES
(29, 0, 11, 'продажам?\nИнтересует не мега-звезда, а именно предметный, руками умеющий продавать сам и помогающий выстраивать это бизнесам.', 459, 0),
(30, 0, 11, '2', 459, 0),
(31, 0, 11, '3', 459, 0),
(36, 0, 11, '3.1', 459, 31),
(37, 1413822798, 11, 'also facing that prob. on the plteform of win 7', 459, 29),
(38, 0, 11, ' here i dont have internet connection.. #Samint Sinha thanks ill check it out maybe tomorrow.', 459, 29),
(39, 0, 11, ' Select max id and you will have dhe last row returned', 459, 29),
(32, 0, 11, '4', 459, 0),
(44, 1414354324, 11, 'How to do', 456, 29),
(45, 1414354469, 11, 'sfsfsf', 456, 29),
(46, 1414354708, 11, 'dddd', 456, 29),
(47, 1414357761, 11, 'sfsfs', 456, 0),
(57, 1414370833, 39, 'kkkppppppp', 456, 0),
(49, 1414358233, 11, 'VSF\nSFSF', 456, 0),
(50, 1414359589, 11, 'How to do', 456, 0),
(51, 1414359660, 11, 'sfsfsdf', 456, 0),
(52, 1414361057, 11, 'SDFSF', 456, 0),
(53, 1414364023, 11, 'dsfdsjfsifmsi', 456, 0),
(54, 1414364031, 11, 'sdfdskjfnskf', 456, 52),
(55, 1414364034, 11, 'sdfdskjfnskf', 456, 52),
(56, 1414364044, 11, 'fndsdfnsofosfi', 456, 52),
(58, 1414370841, 39, 'dfgdfgdgdgdgdgdfgdgdfg', 456, 0);
Result what I need:
Here is example at sqlfiddle: sqlfiddle.com/#!2/dbd82a/1 I need get last 3 rows with a unlimited COMMENTTOARTICLEPID for each first query, if exists. In example, I need get rows with IDCOMMENTTOARTICLE: 58, 57, 56, 52
SELECT a.*
FROM commenttoarticle a
JOIN commenttoarticle b
ON b.CommentToArticlePID = a.idCommentToArticle
ORDER
BY a.idCommentToArticle DESC
LIMIT 3
use IN instead of '=' before sub-select.
USe in instead of ==
SELECT * FROM commenttoarticle a WHERE a.idCommentToArticle in (SELECT CommentToArticlePID FROM commenttoarticle b) ORDER BY a.idCommentToArticle DESC LIMIT 3
Use IN instead of = because your query returns lot of values:
SELECT * FROM commenttoarticle a
WHERE a.idCommentToArticle IN (
SELECT CommentToArticlePID
FROM commenttoarticle b)
ORDER BY a.idCommentToArticle DESC LIMIT 3
You can use limit in both sub query or main query or both.
If you need only 3 result then add limit at main query
SELECT * FROM commenttoarticle a WHERE a.idCommentToArticle IN (SELECT CommentToArticlePID FROM commenttoarticle b) ORDER BY a.idCommentToArticle DESC LIMIT 3
If you want 3 result at sub query then it will be like this
SELECT * FROM commenttoarticle a WHERE a.idCommentToArticle IN (SELECT CommentToArticlePID FROM commenttoarticle b LIMIT 3) ORDER BY a.idCommentToArticle DESC
If you want both
SELECT * FROM commenttoarticle a WHERE a.idCommentToArticle IN (SELECT CommentToArticlePID FROM commenttoarticle b LIMIT 3) ORDER BY a.idCommentToArticle DESC LIMIT 3
I am thinking that your subquery returns more than 1 row hence replace your "=" with "IN". Like this...
SELECT * FROM commenttoarticle a
WHERE a.idCommentToArticle IN (SELECT CommentToArticlePID FROM commenttoarticle b)
ORDER BY a.idCommentToArticle DESC LIMIT 3

multiple WHERE clauses on a joined table

I have a simple application that tracks diners and their favorite flavors and desserts. The records table is just the diner's name and ID, the mid table tracks the desserts and flavors (again by an ID linked to another table of values).
CREATE TABLE IF NOT EXISTS `records` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
INSERT INTO `records` (`id`, `name`) VALUES
(1, 'Jimmy Jones'),
(2, 'William Henry');
CREATE TABLE IF NOT EXISTS `mid` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`diner` int(11) NOT NULL,
`dessert` int(11) NOT NULL DEFAULT '0',
`flavor` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
INSERT INTO `mid` (`id`, `diner`, `dessert`, `flavor`) VALUES
(1, 1, 3, 0),
(2, 1, 2, 0),
(3, 1, 15, 0),
(4, 1, 0, 1),
(5, 2, 3, 0),
(6, 2, 6, 0),
(7, 2, 0, 4),
(8, 1, 34, 0),
(9, 2, 0, 4),
(10, 2, 0, 22);
I'm a little stumped by what should be a simple query-- I want to get all IDs from the records table where certain dessert or flavor requirements are met:
SELECT a.id
FROM records AS a
JOIN mid AS b ON a.id = b.diner
WHERE b.dessert IN (3,2,6)
AND b.flavor IN (4,22)
This query returns no rows, even though there are records that match the where clauses. I am pretty sure I'm missing something obvious with the JOIN but I've tried INNER, OUTER, LEFT and RIGHT with no success.
Can someone put me on the right track and explain what I'm missing?
Thanks
You seem to want diners that have the combinations. Here is one way:
select diner
from records
group by diner
having max(b.dessert = 3) = 1 and
max(b.dessert = 2) = 1 and
max(b.dessert = 6) = 1 and
max(b.flavor = 4) = 1 and
max(b.flavor = 22) = 1
This answers your comment:
select diner
from records
group by diner
having max(case when b.dessert in (2, 3, 6) then 1 esle 0 end) = 1 and
max(case when b.dessert in (4, 22) then 1 else 0 end) = 1
If you are just looking for the records in a that match the conditions, use:
select r.*, d.name
from records r join
diner d
on r.diner = d.id
where b.dessert IN (3,2,6) AND b.flavor IN (4,22)
If this is what you want, the join condition in your query is wrong (a.id should be a.diner).
You SQL statement is fine, but non of your sample records meet your condition, records that would match should look like this
dessert flavor
3 4
3 22
2 4
2 33
6 4
6 22
Non of your input record has any of these combinations
Your WHERE condition does not fit any record in the "mid" table.
There are no records that have dessert in (3, 2, 6) AND flavor in (4, 22), so the query (correctly)returns no result.
You don't have any records that match both where conditions.
( 1, 1, 3, 0) - Matches dessert IN (3,2,6)
( 2, 1, 2, 0) - Matches dessert IN (3,2,6)
( 3, 1, 15, 0)
( 4, 1, 0, 1)
( 5, 2, 3, 0) - Matches dessert IN (3,2,6)
( 6, 2, 6, 0) - Matches dessert IN (3,2,6)
( 7, 2, 0, 4) - Matches flavor IN (4,22)
( 8, 1, 34, 0)
( 9, 2, 0, 4) - Matches flavor IN (4,22)
(10, 2, 0, 22) - Matches flavor IN (4,22)
Perhaps you meant OR?
SELECT a.id
FROM records AS a
JOIN mid AS b ON a.id = b.diner
WHERE b.dessert IN (3,2,6)
OR b.flavor IN (4,22)
Should return 7 results.
Also, your thoughts on JOIN are a red herring. The difference between LEFT and RIGHT is just which table gets precedence when the join clause doesn't match records between them. The difference between INNER and OUTER is just what happens when there isn't a matching record between the two tables. Try this explanative article from coding horror for more details on joins (helpfully pointed out to me in a different SO question, heh).