db查询“ in”似乎已经排序了
两张桌子上有客户(公司)和联系人。联系人和客户都有自己的领域。但是他们都有一个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 -
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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,SQLite表具有ROWID列。我们可以按顺序使用此列。
如果我们希望将它们按顺序插入桌子联系人的顺序,我们可以加入联系人并通过其rowid订购。
请参阅[https://www.sqliteTutorial.net/sqlite-autoincrement/] [1]
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]
db<>fiddle here