Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.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链接在哪里_Mysql_Sql - Fatal编程技术网

MySQL链接在哪里

MySQL链接在哪里,mysql,sql,Mysql,Sql,我有一个包含订单号、客户id和代理id的订单表。然后有一个id为的客户表和一个id为的代理表 我需要从代理id“a03”和代理id“a05”获取所有具有订单的客户id 现在,我正在尝试获取所有客户ID,但仅获取出现在代理“a03”的客户ID列表和代理“a05”列表中的客户ID 将第二个链接到中不起作用 select customer.cid from customer where customer.cid in (select order1.cid from order

我有一个包含订单号、客户id和代理id的订单表。然后有一个id为的客户表和一个id为的代理表

我需要从代理id“a03”和代理id“a05”获取所有具有订单的客户id

现在,我正在尝试获取所有客户ID,但仅获取出现在代理“a03”的客户ID列表和代理“a05”列表中的客户ID

将第二个链接到中不起作用

select customer.cid 
from customer 
where customer.cid in 
    (select order1.cid 
     from order1 
     inner join agent on order1.aid=agent.aid 
     where agent.aid="a05") 
 and 
    (select order1.cid 
    from order1 
    inner join agent on order1.aid=agent.aid 
    where agent.aid="a03");

您需要在中用
或customer.cid替换

select customer.cid 
from customer 
where customer.cid in 
    (select order1.cid 
     from order1 
     inner join agent on order1.aid=agent.aid 
     where agent.aid="a05") 
 or customer.cid in 
    (select order1.cid 
    from order1 
    inner join agent on order1.aid=agent.aid 
    where agent.aid="a03");
或更简短的版本:

select customer.cid 
from customer 
where customer.cid in 
    (select order1.cid 
     from order1 
     inner join agent on order1.aid=agent.aid 
     where agent.aid="a05" or agent.aid="a03") 

我想你只是想要这个,如果我是正确的,就不需要第二个和/或子选择,它只会增加更多的开销

select customer.cid 
from customer 
where customer.cid in 
    (select order1.cid 
     from order1 
     inner join agent on order1.aid=agent.aid 
     where agent.aid="a05" or agent.aid="a03") 

如果你只是想要客户ID,为什么不使用这个呢

select cid from order1 inner join agent on order1.aid = agent.aid where agent.aid in ("a05", "a03");

听起来您只想返回为两个代理出现的客户。如果是这种情况,则可以使用group by:

select c.cid
from customer c
    join order1 o on c.cid = o.cid
    join agent a on o.aid = a.aid
where a.aid in ('a05','a03')
group by c.cid
having count(distinct a.aid) = 2

让我看看我是否做对了

SELECT c.cid 
  FROM customer c
  JOIN order1 o1
    ON o1.cid = c.cid
  JOIN agent a 
    ON a.aid = o1.aid
 WHERE a.aid IN('a05','a03');

?/P>如果你愿意,考虑下面简单的两步过程:1。如果您还没有这样做,请提供适当的DDL(和/或SQLFIDLE),以便我们可以更轻松地复制问题。2.如果您尚未这样做,请提供与步骤1中提供的信息相对应的所需结果集。我认为这将仅获取客户ID,这些ID出现在代理a05或a05的订单中。而不是那些同时使用a03和a05的客户