Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.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:如何在ID不在另一个表中的位置写入select 我有两个表的简单结构:contacts和group_contacts。 联系人可以属于一个、多个或不属于任何组。 我正在尝试写一个select语句,它将为我提供所有联系人 不属于组id“123”的。消极的,不要,让我困惑。 如果不存在“联系人”,则创建表( `联系人id`bigint(20)无符号非空自动增量, `name`varchar(50)不为空) 如果不存在“组\联系人”,则创建表( `联系人id`bigint(20)未签名非空, `组_id`int(11)无符号非空) 谢谢_Mysql - Fatal编程技术网

Mysql:如何在ID不在另一个表中的位置写入select 我有两个表的简单结构:contacts和group_contacts。 联系人可以属于一个、多个或不属于任何组。 我正在尝试写一个select语句,它将为我提供所有联系人 不属于组id“123”的。消极的,不要,让我困惑。 如果不存在“联系人”,则创建表( `联系人id`bigint(20)无符号非空自动增量, `name`varchar(50)不为空) 如果不存在“组\联系人”,则创建表( `联系人id`bigint(20)未签名非空, `组_id`int(11)无符号非空) 谢谢

Mysql:如何在ID不在另一个表中的位置写入select 我有两个表的简单结构:contacts和group_contacts。 联系人可以属于一个、多个或不属于任何组。 我正在尝试写一个select语句,它将为我提供所有联系人 不属于组id“123”的。消极的,不要,让我困惑。 如果不存在“联系人”,则创建表( `联系人id`bigint(20)无符号非空自动增量, `name`varchar(50)不为空) 如果不存在“组\联系人”,则创建表( `联系人id`bigint(20)未签名非空, `组_id`int(11)无符号非空) 谢谢,mysql,Mysql,试试这个 I have a simple structure of 2 tables: contacts and group_contacts. A contact can belong to one, many or no groups. I'm trying to write a select statement that will give me all the contacts that don't belong to group_id '123'. The negative, d

试试这个

I have a simple structure of 2 tables: contacts and group_contacts. A contact can belong to one, many or no groups. I'm trying to write a select statement that will give me all the contacts that don't belong to group_id '123'. The negative, don't, has me confused. CREATE TABLE IF NOT EXISTS `contacts` ( `contact_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL) CREATE TABLE IF NOT EXISTS `group_contacts` ( `contact_id` bigint(20) unsigned NOT NULL, `group_id` int(11) unsigned NOT NULL) thanks
您必须分两个宏步骤进行此操作:

  • 首先,您需要在联系人和group_contacts表之间保留外部联接,以便选择group_contacts表中存在和不存在任何相关关系的所有联系人
  • 然后在group_contacts的
    where
    子句中,应加载组id 123
  • 从联系人a、组联系人b中选择a.contact\u id,其中b.group\u id 123和b.contact\u id=a.contact\u id;
    
    查询中出现语法错误。请检查。我不确定我们是否可以使用“left join b group_contacts”。别名应该在表名之后。我修复了谢谢,输入错误b需要在表名之后。当然,加入条件应始终在form子句中以过滤表乘法。我无法理解您的注释bro!!。。。你所说的格式条款是什么意思??
    select * from contacts a left join  group_contacts b
    on a.contact_id = b.contact_id
    where  b.group_id !=123
    
    select * from contacts as ct 
    left join group_contacts as gc on ct.contact_id=gc.contact_id
    where gc.group_id!=123
    
    select a.contact_id from contacts a, group_contacts b where b.group_id<>123 and b.contact_id=a.contact_id;