0

我有这个 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
4

3 回答 3

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
于 2013-07-08T09:02:39.023 回答
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”更改为您喜欢的任何内容。

于 2013-07-08T09:01:40.333 回答
0

这应该解决它:

(SQL 中需要区分来自选择的不同结果集)

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
于 2013-07-08T09:20:15.927 回答