MySQL查询帮助 - >多个WHERE子句?

时间:2014-10-22 20:58:14

标签: mysql sql

我有如下所示的数据库表。

我要做的是获取colourschemes的所有记录,其中colour_1,colour_2,colour_3,colour_4,colour_5匹配任何用户' agent_id的user_colour_1,user_colour_2或user_colour_3条目。

基本上,用户由代理注册,然后决定他们喜欢什么颜色。 colourschemes(商业用户)自行注册并指定他们所需的颜色。对于特定的代理人,我需要找到任何 colourschemes'为他们选择的用户颜色的任何提供颜色。

----------------------
agents
----------------------
agent_id  | agent_name
--------- | ----------
1         | Rod
2         | Jane
3         | Freddy

------------------------------------------------------------------------------
users
------------------------------------------------------------------------------
user_id | agent_id | user_name | user_colour_1 | user_colour_2 | user_colour_3
------- | -------- | --------- | ------------- | ------------- | -------------
1       | 1        | Andy      | 1             | 2             | 3
2       | 2        | Betty     | 2             | 1             | 5
3       | 3        | Claire    | 3             | 1             | 6
4       | 1        | Derek     | 4             | 5             | 7
5       | 1        | Eric      | 1             | 6             | 2
6       | 2        | Fay       | 2             | 7             | 3
7       | 3        | Gary      | 3             | 1             | 7
------------------------------------------------------------------------------

----------------------------------------------------------------
colourschemes
----------------------------------------------------------------
scheme_id | colour_1 | colour_2 | colour_3 | colour_4 | colour_5
--------- | -------- | -------- | -------- | -------- | --------
1         | 1        | 2        | 3        | 5        | 7
2         | 2        | 3        | 5        | 1        | 4    
3         | 3        | 6        | 7        | 2        | 4    
4         | 4        | 3        | 1        | 7        | 2
5         | 5        | 6        | 7        | 2        | 3
6         | 6        | 5        | 1        | 4        | 3
7         | 7        | 3        | 6        | 1        | 2

-----------------------
colours
-----------------------
colour_id | colour_name
--------- | -----------
1         | red
2         | yellow
3         | blue
4         | green
5         | orange
6         | purple
7         | black

好的,抱歉这么长时间,但这里是想要的结果......

------------------------------------------
results table
------------------------------------------
agent_id | user_id | scheme_id | colour_id
------------------------------------------
1        | 1       | 1         | 1
1        | 1       | 1         | 2
1        | 1       | 1         | 3
1        | 1       | 2         | 2
1        | 1       | 2         | 1
1        | 1       | 2         | 3
1        | 1       | 3         | 3
1        | 1       | 3         | 2
1        | 1       | 4         | 3
1        | 1       | 4         | 1
1        | 1       | 4         | 2
1        | 1       | 5         | 2
1        | 1       | 5         | 3
1        | 1       | 6         | 1
1        | 1       | 6         | 3
1        | 1       | 7         | 3
1        | 1       | 7         | 1
1        | 1       | 7         | 2
1        | 4       | 1         | 5
1        | 4       | 1         | 7
1        | 4       | 2         | 5
1        | 4       | 2         | 4
1        | 4       | 3         | 7
1        | 4       | 3         | 4
1        | 4       | 4         | 4
1        | 4       | 4         | 7
1        | 4       | 5         | 5
1        | 4       | 5         | 4
1        | 4       | 6         | 5
1        | 4       | 6         | 4
1        | 4       | 7         | 7
1        | 5       | 1         | 1
1        | 5       | 1         | 2
1        | 5       | 2         | 2
1        | 5       | 2         | 1
1        | 5       | 3         | 6
1        | 5       | 3         | 2
1        | 5       | 4         | 6
1        | 5       | 4         | 2
1        | 5       | 5         | 6
1        | 5       | 5         | 1
1        | 5       | 6         | 1
1        | 5       | 6         | 2
1        | 5       | 7         | 6
1        | 5       | 7         | 1
1        | 5       | 7         | 2

2 个答案:

答案 0 :(得分:0)

你要找的是一个像这样的复杂where子句。

SELECT 
  u.agent_id, 
  u.agent_name 
from 
  users u, 
  colourschemes c 
where 
  u.user_colour_1 = c.scheme_id  
  or  u.user_colour_2 = c.scheme_id 
  or u.user_colour_3 = c.scheme_id 
  and c.colour_1 = "blue"     
  and c.colour_2 = "purple" 
  and c.colour_3 = "black" 
  and c.colour_4 = "yellow" 
  and c.colour_5 = "green" ;

答案 1 :(得分:0)

嗯,只是为了跟进,这是来自其他建议来源的建议代码......

SELECT u.user_id
     , u.agent_id
     , s.scheme_id
     , u.user_colour_1 AS user_colour
  FROM users u
 INNER JOIN colourschemes ON (u.user_colour_1 = s.colour_1 OR u.user_colour_1 = s.colour_2 OR u.user_colour_1 = s.colour_3 OR u.user_colour_1 = s.colour_4 OR u.user_colour_1 = s.colour_5)
UNION ALL
SELECT u.user_id
     , u.agent_id
     , s.scheme_id
     , u.user_colour_2 AS user_colour
  FROM users u
 INNER JOIN colourschemes ON (u.user_colour_2 = s.colour_1 OR u.user_colour_2 = s.colour_2 OR u.user_colour_2 = s.colour_3 OR u.user_colour_2 = s.colour_4 OR u.user_colour_2 = s.colour_5)
UNION ALL
SELECT u.user_id
     , u.agent_id
     , s.scheme_id
     , u.user_colour_3 AS user_colour
  FROM users u
 INNER JOIN colourschemes ON (u.user_colour_3 = s.colour_1 OR u.user_colour_3 = s.colour_2 OR u.user_colour_3 = s.colour_3 OR u.user_colour_3 = s.colour_4 OR u.user_colour_3 = s.colour_5)