doubt in query - mysql
i am trying this query
SELECT round( avg( DATEDIFF( CURRENT_DATE, age ) /365 ) , 1 )
FROM single_user U
INNER JOIN university_has_single_user T
ON T.single_user_users_id_user = U.users_id_user
where (
SELECT id_uni开发者_运维技巧versity
FROM university
WHERE university = "ISEL"
)
LIMIT 0 , 30
the output will be 26.8 but there is some problem because if i change the name of the university or simply remove the where clause the result is 26.8 again.
the dates are:
1979-06-23
1988-04-23
1988-04-23.
any help? what is the problem?
where (
SELECT id_university
FROM university
WHERE university = "ISEL"
)
should be
where
exists (
SELECT id_university
FROM university x
WHERE x.university = "ISEL" AND
x.universityid = t.universityid
)
Well, I guess. It's not exactly clear to me wat your table structure is, and what you're trying to achieve, but it seems to me that you want to get that number for a given university, passing its name to the query.
If I'm wrong there, please post your table structure and actual goal.
Your WHERE
clause isn't filtering. It's the logical equivalent of WHERE 1=1
One of those two tables has a FK back to table University, does it?
I suspect you're looking for this:
SELECT round( avg( DATEDIFF( CURRENT_DATE, age ) /365 ) , 1 )
FROM single_user U
INNER JOIN university_has_single_user T
ON T.single_user_users_id_user = U.users_id_user
INNER JOIN university AS UNI ON UNI.id =
U.university_id -- or wherever your university ID FK is.
where UNI. university = "ISEL"
LIMIT 0 , 30
The answer will vary depending on your tables schemas.
精彩评论