mysql - 查询将获取 orderID 12345 中所有商品的商品名称和商品类别名称

标签 mysql sql

I need help in writing a query that will get me the item category name for all items in this list. Even if it is null like Item.categoryID it should still give me back the names.

语法是 Select * FROM 然后是表的名称?或许?或者将它们全部连接到一张表中并返回名称。

最佳答案

您可以使用左外连接来完成工作。左外连接基本上获取连接条件左侧表的数据,即使它在另一个表中没有找到匹配项。 为了更好地理解,请遵循此link

enter image description here

假设您想要从绿色圆圈数据中检索数据,即使您没有找到匹配项,也请执行以下操作

select 
         table1.*
       , table2.*
from 
     table1 left join table2 on table1.columnname = table2.columnname

因为您对获取类别名称更感兴趣,所以我建议您

select 
        cat.categoryname
      , item.name
from 
      category as  cat left join item as item on cat.categoryID = item.categoryID
group by cat.categoryname, item.name

通过这种方式,您可以获得所有类别名称以及这些类别中的项目,并找到空类别。

关于mysql - 查询将获取 orderID 12345 中所有商品的商品名称和商品类别名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34952803/

相关文章:

MySQL - 带有内连接的 SQL 查询花费太长时间

php - 使用 php 更新 MySQL 中的日期行和时间行

mysql - 如何在MySql中获取有时间限制的数据

MySQL删除尾随零

sql - 在 Excel VBA 中使用 SQL 进行随机化和采样

SQLite 和多次写入

mysql - 向列添加一个值,指示一条记录来自哪个表+sql

mysqldump 在 perl 脚本中抛出错误,但在脚本之外运行良好

mysql - MySQL InnoDB 适合这种场景吗?

php - 如何从蛋糕查询数组中获取正常的 SQL 查询?