我需要从一个表中返回所有值,然后将此表连接到另一个查询而不重复值。到目前为止,这是我的查询。
CREATE TABLE tmp_tbl (Id INT(11) PRIMARY KEY, Per VARCHAR(30), Des VARCHAR(50), Rol INT(11));
INSERT INTO tmp_tbl(Id, Per, Des, Rol)
SELECT DISTINCT
p.p_id AS Id,
p.p_title AS Per,
p.p_description AS Des,
'' AS Rol
FROM perm p;
SELECT
p.p_id AS Id,
p.p_title AS Per,
p.p_description AS Des,
r.id AS Rol
FROM perm p
LEFT JOIN roles rp
ON p.p_id = rp.p_id
INNER JOIN rol r
ON r.id = rp.r_id AND rp.r_id IN (101)
UNION ALL
SELECT * FROM
tmp_tbl;
DROP TABLE tmp_tbl;
该语句返回第一个 select 中的所有行,但复制了第二个 select 语句中 union 上的行。