简体   繁体   English

如何从一个表的字段中获取其主键在另一个表中的2个可能字段中

[英]How to get fields from a table where its primary key is used in another table in 2 possible fields

I have 2 sql tables 我有2个sql表

    CREATE TABLE users(userID INTEGER PRIMARY KEY AUTOINCREMENT, alias TEXT NOT NULL)
    CREATE TABLE friends(userID1 INTEGER, userID2 INTEGER, PRIMARY KEY(userID1, userID2))

When 2 users want to be friends, I create a new relationship btw the 2 by inserting a row in the friends table. 当2个用户希望成为朋友时,我通过在friends表中插入一行来创建2之间的新关系。 The order for the userID of the 2 friends in the row do not matter but it should be unique, whatever the order. 该行中2个朋友的userID的顺序无关紧要,但是无论顺序如何,它都应该是唯一的。

Question 1: My second table do not currently reflect what I want. 问题1:我的第二张桌子目前未反映我想要的内容。 I mean I can do this and it's fine while I don't want it to be: 我的意思是我可以做到,但我不希望这样做是很好的:

    INSERT INTO friends VALUES(1,2);
    INSERT INTO friends VALUES(2,1);

Is it possible to enforce this constraint in my sql table, or should I check with a query if there is not already a relationship in the reversed order. 是否可以在我的sql表中强制执行此约束,或者是否应该以查询方式检查查询是否还没有反向关系。

Question 2: What is the most optimal sql query to get the list of friends ( userID and alias ) if I have a userID , if I have for example: 问题2:如果我有一个userID ,那么例如,如果要获取朋友列表( userIDalias ),最最佳的sql查询是什么:

    INSERT INTO users VALUES(1, 'A');
    INSERT INTO users VALUES(2, 'B');
    INSERT INTO users VALUES(3, 'C');
    INSERT INTO users VALUES(4, 'D');
    INSERT INTO friends VALUES(1, 2);
    INSERT INTO friends VALUES(2, 3);
    INSERT INTO friends VALUES(4, 1);
    INSERT INTO friends VALUES(2, 4);

I want: 我想要:

For userID=1: (2, B) and (4, D)
For userID=2: (1, A), (3, C) and (4, D)
For userID=3: (2, B)
For userID=4: (1, A) and (2, B)

Bonus question: Can I improve the tables to better reflect what I want? 奖励问题:我可以改进表格以更好地反映我想要的吗?

Thanks a lot! 非常感谢!

One question at a time. 一次提出一个问题。 Change your insert from this structure: 从此结构更改插入:

insert into table
(field1, field2, etc)
values
(value1, value2, etc)

to this structure 这个结构

insert into table
(field1, field2, etc)
select distinct value1, value2, etc
from someSmallTable
where not exists
(subquery to check for existing rows)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接到主键以外的其他表 - Joining to another table on fields other than its primary key 如何创建其中一个字段不是主键的关联表? - How to create associative table where one of the fields is not primary key? 如何创建表,其中第一个表中的列是另一个表中的字段? - How to create table where columns in first table are fields in another table? 合并SQL表主键的字段? - Combining fields for SQL table primary key? 从表中选择字段,其中id不在mysql的另一个表中[不起作用] - select fields from table where id not in another table in mysql [not working] 在实体框架核心中,当两个字段映射到另一个表的主键时,我们如何为表设置关系 - In entity framework core, how we can set relationship for a table, when two fields is mapping to primary key of the another table 如何制作这个复杂的 select 语句,其中 WHERE 条件查看另一个表并连接第三个表中的字段 - How to make this complex select statement with WHERE condition looking into another table and with joined fields from third table 带有来自另一个表的主键的sql表 - sql table with primary key from another table 具有来自另一个表的主键的临时表 - Temporary table with Primary Key from another table 将字段从一个表复制到另一个字段匹配的表 - Copy field from one table to another where fields match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM