Mysql子查询不显示行

时间:2015-05-06 17:41:02

标签: 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,来自子查询的abonementas?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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