错误消息 - 每个派生表必须具有自己的别名

时间:2013-07-08 08:59:49

标签: sql distinct alias

我有这个SQL语法,但它无法正常工作并收到此错误:

  

“#1248 - 每个派生表必须有自己的别名”。

你能帮帮我吗?

SELECT *
FROM produse_comenzi
JOIN comenzi ON comenzi.id_comanda = produse_comenzi.id_comanda
JOIN (SELECT DISTINCT numar_factura FROM facturi)
ON facturi.id_comanda = comenzi.id_comanda

3 个答案:

答案 0 :(得分:1)

在第二个连接中,您正在使用子查询但是您没有给结果一个别名,即通过

来识别结果的东西
SELECT *
  FROM produse_comenzi
  JOIN comenzi 
    ON comenzi.id_comanda = produse_comenzi.id_comanda
  JOIN (SELECT DISTINCT numar_factura FROM facturi) -- has no alias
    ON facturi.id_comanda = comenzi.id_comanda

你应该做

SELECT *
  FROM produse_comenzi
  JOIN comenzi 
    ON comenzi.id_comanda = produse_comenzi.id_comanda
  JOIN (SELECT DISTINCT numar_factura, id_comanda FROM facturi) AS facturi
    ON facturi.id_comanda = comenzi.id_comanda

答案 1 :(得分:0)

您必须为每个被视为表格的子查询添加别名:

SELECT *
FROM produse_comenzi
JOIN comenzi ON comenzi.id_comanda = produse_comenzi.id_comanda
JOIN (SELECT DISTINCT numar_factura FROM facturi) x
ON x.id_comanda = comenzi.id_comanda

这里我已经命名了结果集x并在连接条件中引用了它。您可以将“x”更改为您喜欢的任何内容。

答案 2 :(得分:0)

这应该解决它:

(在SQL中需要区分不同的Resultset与选择)

SELECT * 
FROM produse_comenzi AS table_1  
JOIN comenzi AS table_2  
ON table_2.id_comanda = table_1.id_comanda  
JOIN (SELECT DISTINCT numar_factura FROM facturi AS table_3)  
ON table_3.id_comanda = table_2.id_comanda