Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从表中获取结果的SQL查询_Mysql_Sql - Fatal编程技术网

Mysql 从表中获取结果的SQL查询

Mysql 从表中获取结果的SQL查询,mysql,sql,Mysql,Sql,非常感谢你的帮助 我有一个下表,由服务id和艺术家id两列组成。主键是(服务id,艺术家id)。我想检索一组服务ID的所有艺术家 Sample Table : service_id artists_id 5 9 6 9 5 10 1 9 5 1 6 1 6 7 1 10 样本表: 服

非常感谢你的帮助

我有一个下表,由服务id和艺术家id两列组成。主键是(服务id,艺术家id)。我想检索一组服务ID的所有艺术家

Sample Table : service_id artists_id 5 9 6 9 5 10 1 9 5 1 6 1 6 7 1 10 样本表: 服务\u id艺术家\u id 5 9 6 9 5 10 1 9 5 1 6 1 6 7 1 10 我试过了,但它不起作用,因为它给了所有提供服务id 5或服务id 6或服务id 1的艺术家

SELECT artists_id FROM `service_schedule` WHERE service_id IN (5, 6, 1) 从“服务计划”中选择艺术家id,其中服务id位于(5、6、1) 我想要提供服务id 5、服务id 6和服务id 1的艺术家。对于上述示例表,只有artist 9给出了集合(5,6,1)中的所有3项服务

因此,如果我想检索所有能够提供id为5、6和1的服务的艺术家。如何编写SQL查询?

尝试一下

select artist_id from table where service_id IN (5, 6) AND  
artist_id IN (select artist_id from table group by artist_id having count(*) > 1);
我正在获取计数大于1的所有艺术家id,并且必须是具有5和6的服务id

我希望这会有所帮助

编辑

select artists_id from Sample where service_id IN (5, 6, 1)  
AND  
artists_id IN (select artists_id from Sample group by artists_id  
having count(*) > 2) group by artists_id;
select * from Sample where service_id IN (5, 6, 1, 8) and is_available = 1
AND  
artists_id IN (select artists_id from Sample where is_available = 1 group by artists_id  
having count(*) > 3) group by artists_id;
您可以获得服务ID,如5,6或5,6,1,您只需计算服务ID,然后
将该值-1保留在2的位置

编辑

select artists_id from Sample where service_id IN (5, 6, 1)  
AND  
artists_id IN (select artists_id from Sample group by artists_id  
having count(*) > 2) group by artists_id;
select * from Sample where service_id IN (5, 6, 1, 8) and is_available = 1
AND  
artists_id IN (select artists_id from Sample where is_available = 1 group by artists_id  
having count(*) > 3) group by artists_id;
试试这个:

SELECT DISTINCT(s1.artists_id) FROM service_schedule s1
JOIN service_schedule s2 ON s2.service_id = s1.service_id AND s2.artists_id  <> s1.artists_id
WHERE s1.service_id IN (5, 6, 1)
从服务计划s1中选择不同的(s1.艺术家id)
加入s2.service\u id=s1.service\u id和s2.Artisters\u id s1.Artisters\u id上的service\u计划s2
其中s1.service_id在(5,6,1)中

我对这个问题不太清楚,但如果我理解清楚,下面的解决方案对您有效

select s1.service_id, s1. artists_id 
from Sample s1 inner join Sample s2
on s1.artists_id = s2.artists_id 
where s2.service_id = 5 and s1.service_id = 6
union
select s1.service_id, s1. artists_id 
from Sample s1 inner join Sample s2
on s1.artists_id = s2.artists_id 
where s2.service_id = 6 and s1.service_id = 5

示例输出-

您必须使用分组查询:

select
  artist_id
from
  service_schedule
group by
  artist_id
having
  sum(service_id in (5,6,1))=3

(5,6,1)中的
服务id
在条件为真时将被评估为1,您需要检查每个艺术家id的总和是否为3(所有三项服务都提供-
arist\u id
并且
服务id
是唯一的,因此无需检查此处的重复行)

请阅读并详细说明“不工作”。谢谢。我会看的伙计你想说什么?或者告诉我你们有并没有收到我的查询?先告诉我,然后我们再谈。我会解释问题:OP想问的是,查询应该得到同时属于服务ID 5和6的艺术家ID,而不是5或两者。这意味着,计数必须大于0,对吗?简言之,艺术家id(服务id为5和6)与我解释的mate相同:)感谢游乐场伙伴!你会很快得到答案的,伙计,首先你给出了5,6,然后是5,6,1,这些服务ID是动态的吗?你在用php吗?一旦检查我的编辑部分。在你编辑问题之前,它是有效的,然后你做了5,6,1在这种情况下,你需要计算2@Yousaf现在修改答案检查。感谢您的回复和时间。很抱歉,我再次编辑了这个问题。你能再试一次吗?谢谢你的回复。成功了。现在我又增加了一个专栏。如果我只想要那些提供服务集(5,6,1)且其is_available=1的艺术家,如何修改查询。请穿过这个操场@MadhanShah
。。。从service\u schedule where is\u available=1 groupby…
,但我认为is\u available标志应该放在另一个表中,例如艺术家。。。然后你需要加入