SELECT ... IN 复制结果集中的记录?
您知道如何编写这样的查询:
SELECT * FROM customers WHERE id IN ( 1, 3, 1, 1 )
还返回重复的记录(即: id 为 1 的 3 个客户)吗?
另一个问题 - 有没有办法让记录按 IN 子句中 id 的顺序排序?
编辑: ID为PK,不存在多个具有相同ID的记录。我希望结果集包含 ID 为 1 的记录,乘以 3 次
谢谢
Do you know how to write a query like:
SELECT * FROM customers WHERE id IN ( 1, 3, 1, 1 )
to return also the records that are duplicate (ie: 3 customers with id 1)?
another question - is there a way I can get the records sorted by the order of the ids in the IN clause?
Edit:
The ID is PK, there are no multiple records with the same ID. I want the resultset to contain the record with ID 1, multiplied 3 times
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将列表条目插入(临时)表中吗?如果是这样,
要解决排序问题,您需要按升序插入它们,例如“排名”
Can you insert the list entries into a (temporary) table? if so
To solve the ordering you'd need insert them in order with an ascending numeric value e.g. 'rank'
对于你的第一个问题,这应该有效:
SELECT * FROMcustomers WHERE id IN (1, 3);
它将返回所有具有 id=1 或 id=3 的客户,无论有多少人共享相同的信息ID。对于第二个问题:
字段中的值应与 IN 子句中的值相同。
for your first question this should work:
SELECT * FROM customers WHERE id IN (1, 3);
it will return all customer that have id=1 or id=3 no matter how many are there share same id.for second question:
Values in Field should be same as in IN clause.
如果你有 3 个客户具有相同的 id,你就会遇到更大的问题(假设 id 是 PK)。在某些时候您会遇到一些数据完整性问题。
If you have 3 customers with the same id, you have bigger problems (assuming id is the PK). You will run into some data integrity issues at some point.
您可以构建一个内联表,并将其
连接
到基表:You could build an inline table, and
join
it to the base table:您好,如果表中的 id 是主键,则无法拥有更多具有 id 的客户,但如果不是,它将返回 id=1 的所有客户
HI, if your id in table is primary key then there is no way to have more customers with id, but in case its not than it will return all customers with id=1