SQL 选择并根据所选值列出列表

发布于 2024-12-22 13:14:42 字数 522 浏览 0 评论 0原文

我很难想出一个标题,所以我希望这有点可以理解......

我有下表:

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 技术交流群。

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

发布评论

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

评论(1

明媚如初 2024-12-29 13:14:42

items 表中有 customer_id 外键吗?如果是这样,您可以执行以下操作:

SELECT customers.name AS customer_name, item.name AS item_name FROM customers
INNER JOIN items ON items.customer_id=customers.id;

visibleItems 不会控制每个项目列出的客户数量 - 外键关系和联接可以做到这一点。

有关各种连接类型的详细解释,请参阅此处: SQL 连接的直观解释

Do you have a customer_id foreign key in the items table? If so, you could do something like this:

SELECT customers.name AS customer_name, item.name AS item_name FROM customers
INNER JOIN items ON items.customer_id=customers.id;

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.

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