SQL 合并具有匹配列的表
SQL merge tables with matching columns
如何将具有相同列名的 2 个 table 合并为 1 个 table?像这样:
第2个table应该填第1个table.
这是我得到的最接近的结果
SELECT * FROM
Animals
LEFT JOIN Best
ON Animals.species=Best.species;
http://sqlfiddle.com/#!5/d0a98/3
但它似乎连接了第二个 table。
LEFT JOIN 真的是正确的方法吗?
您应该在 SELECT
中列出列。然后你会很容易地看到你需要的只是 COALESCE()
:
SELECT a.price, a.species, COALESCE(b.name, a.name) as name
FROM Animals a LEFT JOIN
Best b
ON a.species = b.species;
如何将具有相同列名的 2 个 table 合并为 1 个 table?像这样:
第2个table应该填第1个table.
这是我得到的最接近的结果
SELECT * FROM
Animals
LEFT JOIN Best
ON Animals.species=Best.species;
http://sqlfiddle.com/#!5/d0a98/3
但它似乎连接了第二个 table。
LEFT JOIN 真的是正确的方法吗?
您应该在 SELECT
中列出列。然后你会很容易地看到你需要的只是 COALESCE()
:
SELECT a.price, a.species, COALESCE(b.name, a.name) as name
FROM Animals a LEFT JOIN
Best b
ON a.species = b.species;