SQL 选择并根据所选值列出列表
我很难想出一个标题,所以我希望这有点可以理解......
我有下表:
items: id|name|...|visibleItems
例如它包含:
1|test1|...|2
2|test2|...|1
3|test3|...|2
然后将其与另一个表(客户)连接,我想将其列出如下:
cust1|test1|...
cust2|test1|...
cust3|test2|...
cust4|test3|...
cust4|test3|...
...因此使用“visibleItems”作为列出的项目的编号。
我的 SQL 很简单,例如:
SELECT [values] FROM [tables] WHERE [statements] ORDER BY name
这有意义吗?如果是这样,该怎么办? :)
I had a hard time coming up with a title, so I hope this is somewhat understandable...
I have the following table:
items: id|name|...|visibleItems
E.g. it contains:
1|test1|...|2
2|test2|...|1
3|test3|...|2
This is then joined with another table (customers) and I would like to list it as follows:
cust1|test1|...
cust2|test1|...
cust3|test2|...
cust4|test3|...
cust4|test3|...
...thus using the 'visibleItems' as the number for items listed.
The SQL I have is something simple like:
SELECT [values] FROM [tables] WHERE [statements] ORDER BY name
Does this make sense? If so, what to do? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
items 表中有 customer_id 外键吗?如果是这样,您可以执行以下操作:
visibleItems
不会控制每个项目列出的客户数量 - 外键关系和联接可以做到这一点。有关各种连接类型的详细解释,请参阅此处: SQL 连接的直观解释。
Do you have a customer_id foreign key in the items table? If so, you could do something like this:
visibleItems
wouldn't control how many customers are listed per item--the foreign key relationship and join would do that.See here for a decent explanation of the various join types: A Visual Explanation of SQL Joins.