I have a simple query
SELECT
gruppo.*, comuni.Nome as nomecomune
FROM
gruppo, comuni
WHERE
(comuni.ID_Comuni = gruppo.ID_Comuni or gruppo.ID_Comuni ='0')
AND ID_Gruppo='3'
How you can understand from where cause I need to always show a row where ID_Gruppo='3'
, but if gruppo.ID_Comuni='0'
(so if there is not a link) I need to don't SELECT comuni.Nome
column.
The above query works, but if there is not a link between tables ( gruppo.ID_Comuni=0
) show anyway a (random?) comuni.Nome
value.
So, I need to do something like this:
SELECT
gruppo.*,`
if(gruppo.ID_Comuni <> '0') then also select `comuni.Nome
FROM
gruppo, comuni
WHERE
(comuni.ID_Comuni = gruppo.ID_Comuni OR gruppo.ID_Comuni = '0') AND ID_Gruppo = '3'
I hope I have been clear, thank you for your help.
It is not possible, to write an SELECT column ONLY IF... FROM ..
You are also don't getting random results - you query is performing some kind of cross-join, where it joins each row from the table comuni
to each row from table gruppo
, where gruppo.ID_Comuni = 0
.
If you need a "select only if.."-command, you have to write a stored procedure, or an sql-script.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.