简体   繁体   中英

Assign a value to a field on SELECT statement

im not sure if im doing well with this field calendar.type_event = NULL . Now works well to me because I need to differentiate the second SELECT as a third type.

I want to do something like calendar.type_event = 'shared_event' to differentiate it. But returns a 0, with NULL returns nothing. This is not the problem only I need to know if I can assign my own value: 'shared_event'.

Thanks a lot

The entire query is:

(SELECT     
    id, type_event
    FROM calendar
    WHERE user_id = '.$user_id.'        
)       

 UNION

(SELECT 
    calendar.id, calendar.type_event = NULL
    FROM calendar
    RIGHT JOIN avisos ON avisos.app_id = calendar.id 
    WHERE avisos.user_destiny_id = '.$user_id.'
)

ORDER BY fecha_evento ASC

You should be able to do something like this:

(SELECT     
    id, type_event
    FROM calendar
    WHERE user_id = '.$user_id.'        
)       

 UNION

(SELECT 
    calendar.id, "shared_event"
    FROM calendar
    RIGHT JOIN avisos ON avisos.app_id = calendar.id 
    WHERE avisos.user_destiny_id = '.$user_id.'
)

ORDER BY fecha_evento ASC

I need to know if I can assign my own value: 'shared_event' .

Yes, you can. In place of column calendar.type_event just use literal 'shared_event'

Example :

SELECT 
       calendar.id, 'shared_event'
  FROM calendar
       RIGHT JOIN avisos ON avisos.app_id = calendar.id 
 WHERE avisos.user_destiny_id = '.$user_id.'

I think this is what you are looking for:

(SELECT     
    id, type_event
    FROM calendar
    WHERE user_id = '.$user_id.'        
)       
UNION
(SELECT 
    calendar.id, '' type_event
    FROM calendar
    RIGHT JOIN avisos ON avisos.app_id = calendar.id 
    WHERE avisos.user_destiny_id = '.$user_id.'
)
ORDER BY fecha_evento ASC

In the second query, type_event field will contain null values. If the type_event field is a numeric field, you can put 0 (ZERO) instead of '' (empty single quotes/string).

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM