问题
Hello,
I have the following sql table + data:
CREATE TABLE IF NOT EXISTS `job_requires` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`Job_id` bigint(20) unsigned NOT NULL,
`Group_Index` int(11) NOT NULL,
`Field_id` bigint(20) unsigned NOT NULL,
`Field_Value` int(11) NOT NULL,
PRIMARY KEY (`id`)
)
INSERT INTO `job_requires`
(`id`, `Job_id`, `Group_Index`, `Field_id`, `Field_Value`)
VALUES
(1, 7, 1, 11, 50),
(2, 7, 1, 14, 50),
(3, 7, 1, 11, 59),
(4, 7, 2, 14, 1),
(5, 7, 2, 11, 2),
(6, 8, 1, 14, 55),
(7, 8, 1, 11, 50),
(8, 8, 1, 14, 59),
(9, 8, 2, 11, 60),
(10, 8, 2, 14, 61);
I need to get all 'Job_id' by filtering that based on the 'Field_id'+'Field_Value' fields. I want the query to be somthing like:
SELECT job_requires.Job_id
FROM job_requires
WHERE (Field_id=11 AND Field_Value=50) AND (Field_id=14 AND Field_Value=1)
My problem is that I want query to be based also on the Group_Index
field:
Only if 'Job_id' in my previous query have both conditions, and those conditions have the same Group_Index
It should be in the query's result.
UPDATE: I don't know how many filters the query will have, it could be also:
SELECT job_requires.Job_id
FROM job_requires
WHERE (Field_id=11 AND Field_Value=50) AND (Field_id=14 AND Field_Value=1) AND (Field_id=16 AND Field_Value=56) AND (Field_id=12 AND Field_Value=26)
I hope you understand my problem, because my english is not great :(
Thank you very much
回答1:
SELECT j1.Job_id
FROM job_requires j1
INNER JOIN job_requires j2
ON j1.Job_id = j2.Job_id
AND j1.Group_Index = j2.Group_Index
WHERE j1.Field_id = 11 AND j1.Field_Value = 50
AND j2.Field_id = 14 AND j2.Field_Value = 1
回答2:
Obviously the following is impossible:
WHERE (Field_id=11 AND Field_Value=50) AND (Field_id=14 AND Field_Value=1)
Because Field_id cannot be 11 and 14 at the same time, a field in a row can only hold one value.
So this should read:
WHERE (Field_id=11 AND Field_Value=50) OR (Field_id=14 AND Field_Value=1)
If you want to combine the group_index
in there you should include that in the inner brackets:
SELECT
job_requires.Job_id
FROM job_requires
WHERE (Field_id=11 AND Field_Value=50 AND Group_Index = 1)
OR (Field_id=14 AND Field_Value=1 AND Group_index = 2)
This should work.
If you want to select a cluster of job_id's then you should do a self-join like in @Joe's answer.
回答3:
SELECT job_requires.Job_id, COUNT( DISTINCT Group_Index ) AS group_index_count
FROM job_requires
WHERE (Field_id=11 AND Field_Value=50) OR (Field_id=14 AND Field_Value=1)
HAVING group_index_count = 1
I'm not quite sure if I fully understood what you want to do, but I'm guessing this might do the job.
回答4:
Here's one way of doing it... join the table to itself:
SELECT j1.Job_id
FROM job_requires j1
INNER JOIN job_requires j2
ON j1.Job_id = j2.job_id
AND j1.Field_id=11 AND j1.Field_Value=50 AND j2.Field_id=14 AND j2.Field_Value=1
Update I looked at this and now I like this better, because it avoids the join.
SELECT j1.Job_id
FROM job_requires j1
WHERE j1.Field_id=11 AND j1.Field_Value=50
AND (SELECT COUNT(*)
FROM job_requires j2
WHERE j2.Job_id = j1.Job_id AND j2.Field_id=14 AND j2.Field_Value=1) > 0
Update #2 If you have additional parameters, you can just add select count(*) statements:
SELECT j1.Job_id
FROM job_requires j1
WHERE j1.Field_id=11 AND j1.Field_Value=50
AND (SELECT COUNT(*)
FROM job_requires j2
WHERE j2.Job_id = j1.Job_id AND j2.Field_id=14 AND j2.Field_Value=1) > 0
AND (SELECT COUNT(*)
FROM job_requires j3
WHERE j3.Job_id = j1.Job_id AND j3.Field_id=15 AND j3.Field_Value=98) > 0
AND (SELECT COUNT(*)
FROM job_requires j4
WHERE j4.Job_id = j1.Job_id AND j4.Field_id=16 AND j4.Field_Value=11) > 0
来源:https://stackoverflow.com/questions/7150321/mysql-select-query-conditions