2015-05-06 71 views
0

這是我的查詢:Mysql的子查詢沒有顯示行

SELECT a.vardas, a.pavarde FROM `y6fdt_igym_abonementai` AS a 
INNER JOIN 
( 
    SELECT max(galiojaiki) gal, id, abonementas FROm y6fdt_igym_sutartys 
) c 
on c.abonementas = a.id 

爲什麼我只看到a.vardas和a.pavarde列作爲結果。但不是id,subquery的abonementas?

+0

只是爲了您的信息。你檢查了子查詢的結果嗎?因爲在其他字段沒有「group by」的那個「max」可以按照預期給你另一個結果。 – stevenvanc

回答

5

如果您想查看這些列,則必須將它們添加到主要的SELECT子句中。

SELECT a.vardas, a.pavarde, c.gal, c.id, c.abonementas 
FROM `y6fdt_igym_abonementai` AS a 
INNER JOIN 
( 
    SELECT max(galiojaiki) gal, id, abonementas FROm y6fdt_igym_sutartys 
) c 
on c.abonementas = a.id 

注意,在子查詢的idabonementas列將無法從一行max(galiojaiki)。它會從表格中隨機選取一行。如果你想那些特定的值,你需要使用:

SELECT a.vardas, a.pavarde, c.gal, c.id, c.abonementas 
FROM `y6fdt_igym_abonementai` AS a 
FROM y6fdt_igym_sutartys AS c ON c.abonementas = a.id 
INNER JOIN 
( 
    SELECT max(galiojaiki) AS maxgal 
    FROm y6fdt_igym_sutartys 
) d on c.galiojaiki = d.maxgal 
0

變化

SELECT a.vardas, a.pavarde FROM `y6fdt_igym_abonementai` AS a 
... 
SELECT max(galiojaiki) gal, id, abonementas FROm y6fdt_igym_sutartys 

SELECT a.vardas, a.pavarde FROM `y6fdt_igym_abonementai` a 
... 

SELECT max(c.galiojaiki) c.gal, c.id, c.abonementas FROm y6fdt_igym_sutartys c