【问题标题】:sql statement to select all from another table [closed]sql语句从另一个表中选择所有[关闭]
【发布时间】:2018-11-20 22:27:49
【问题描述】:

列出表格

id user_id list_name
1  1       test
2  1       test2

项目表

id list_id item_name price_item item_checked
1  1       apple     2          0
2  2       orange    2          0

结果应该是

id item_name price_item item_checked
1  Apple     2           0

我将如何从列表 1 中选择全部

【问题讨论】:

  • 提示:JOIN,WHERE。
  • 到目前为止你尝试了什么?

标签: mysql sql


【解决方案1】:

你的问题不清楚,但我会尽力回答。

要获得您发布的结果:

select *
from Items_Table

标题说从不同的表中选择所有。我假设列表是第一个表,项目是您所指的“不同”表。

Select Items.*
from Lists_Table Lists
left join Items_Table Items on Items.list_id = Lists.id

现在,假设您正在寻找特定的项目,例如列表表中 ID 为 1 的位置。然后你会得到以下内容:

Select Items.*
from Lists_Table Lists
left join Items_Table Items on Items.list_id = Lists.id
where Lists.id = 1

您可以将 where 语句中的 Lists.id 替换为任何其他列,并将其设置为您要查找的值。

我希望这会有所帮助。请尝试更清楚地了解您要查找的内容!

祝你好运!


【讨论】:

  • JOIN 条件应该是items.list_id = lists.id 是一个相当安全的赌注
  • 谢谢@Nick。我进行了适当的更改。
猜你喜欢
  • 2013-12-29
  • 2019-06-03
  • 2020-09-09
  • 1970-01-01
  • 2019-05-04
  • 2014-04-14
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多