Mysql 从第二个表返回类别(如果存在)
我有两个表格,一个是标准类别,一个是编辑类别。如果表2中存在已编辑的类别,则应从中提取标题。我是使用JOIN还是如何使用Mysql 从第二个表返回类别(如果存在),mysql,sql,Mysql,Sql,我有两个表格,一个是标准类别,一个是编辑类别。如果表2中存在已编辑的类别,则应从中提取标题。我是使用JOIN还是如何使用 Table 1: id, title Table 2: id, parent_id, title 内容: Table 1 id: 1 title: cat1 id: 2 title: cat2 Table 2 id: 1 parent_id: 1 title: Category 1 返回: id: 1 title: Category 1 id: 2 title: cat
Table 1: id, title
Table 2: id, parent_id, title
内容:
Table 1
id: 1 title: cat1
id: 2 title: cat2
Table 2
id: 1 parent_id: 1 title: Category 1
返回:
id: 1 title: Category 1
id: 2 title: cat2
谢谢我找到了这个。它会做完全相同的事情还是不同的事情?合并b.标题,a.标题作为标题
SELECT
a.id,
CASE
WHEN b.title IS NULL THEN
a.title
ELSE
b.title
END as title
FROM
t1 a
LEFT JOIN t2 b ON a.id = b.parent_id;