db查询“ in”似乎已经排序了

发布于 2025-01-27 05:50:36 字数 967 浏览 2 评论 0原文

两张桌子上有客户(公司)和联系人。联系人和客户都有自己的领域。但是他们都有一个ID是主要关键。此外,客户端和联系人之间存在“链接” - 每个客户端都可以分配一个联系人,反之亦然。有第三个表,称为“分配”,它只有两个字段 - 公司ID和联系人ID。

假设我们已将与ID 3的联系人链接到具有ID 12076的公司。接下来,我们与ID 10的联系人链接了一个与ID 5的联系人。Check我们的结果:

SELECT contactId FROM assignment WHERE companyId = 12076;

结果是正确的 -

在这些步骤之后,在某个时候,我需要获得与我需要的公司相关联的联系领域。有一家12076公司,我需要获取与之相关联的所有联系字段。我提出了这样的请求:

SELECT id, fullName FROM contacts WHERE contacts.rowid IN (SELECT contactId FROM assignment WHERE companyId = 12076);

此请求显示我需要的联系人及其字段。恰好3个联系人,但存在一个问题 - 它们以排序顺序显示,以ID为单位。

我需要以添加的顺序显示联系字段。我该如何涂鸦?也许我需要使用加入?

There are clients(companies) and contacts in two tables. Both contacts and clients have their own fields. But they all have an id that is a primaryKey. In addition, there are "links" between clients and contacts - each client can be assigned a contact and vice versa. There is a third table for this, called 'assignment', which has only two fields - company id and contact id.

Suppose we have linked a contact with id 3 to a company with id 12076. Next, we have linked a contact with id 10, and the last contact with id 5. Сheck our result:

SELECT contactId FROM assignment WHERE companyId = 12076;

The result is correct -

enter image description here

After these steps, at some point I need to get the FIELDS of contacts associated with the company I need. There is a company 12076, I need to get all the contact fields associated with it. I made a request like this:

SELECT id, fullName FROM contacts WHERE contacts.rowid IN (SELECT contactId FROM assignment WHERE companyId = 12076);

This request displays the contacts I need and their fields. Exactly 3 contacts, but there is one problem - they are displayed in sorted order, sorted by id.
enter image description here

I need the contact fields to be displayed in the order in which they were added. How can i dow this? Maybe i need to use JOIN?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美羊羊 2025-02-03 05:50:36

默认情况下,SQLite表具有ROWID列。我们可以按顺序使用此列。
如果我们希望将它们按顺序插入桌子联系人的顺序,我们可以加入联系人并通过其rowid订购。
请参阅[https://www.sqliteTutorial.net/sqlite-autoincrement/] [1]

 创建表公司(ID int);
创建表触点(ID int);
创建表分配(CompanyID INT,ContactID INT);
插入公司价值(12076);
插入触点值(10),(3),(5);
插入分配值(12076,3),(12076,10),(12076,5);
 
 选择a.contactid 
从分配a
加入联系人c
在a.contactid = c.id
companyId = 12076
C.Rowid的订单;
 
 | ContactID |
| --------:|
| 10 |
| 3 |
| 5 |

db<>>

By default an SQLite table has a Rowid column. We can use this column in ORDER BY.
If we want the ContactId's in the order they were inserted into the table contacts we can join onto contacts and order by it's Rowid.
See [https://www.sqlitetutorial.net/sqlite-autoincrement/][1]

create table companies(id int);
create table contacts (id int);
create table assignment(companyId int,contactId int);
insert into companies values(12076);
insert into contacts values(10),(3),(5);
insert into assignment values (12076,3),(12076,10),(12076,5);
SELECT a.contactId 
FROM assignment a
JOIN contacts c
on a.contactId = c.id
WHERE companyId = 12076
order by c.Rowid;
| contactId |
| --------: |
|        10 |
|         3 |
|         5 |

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文