简体   繁体   English

为SELECT语句中的字段分配值

[英]Assign a value to a field on SELECT statement

im not sure if im doing well with this field calendar.type_event = NULL . 我不确定即时通讯是否可以很好地处理calendar.type_event = NULL Now works well to me because I need to differentiate the second SELECT as a third type. 现在对我来说效果很好,因为我需要将第二个SELECT区分为第三个类型。

I want to do something like calendar.type_event = 'shared_event' to differentiate it. 我想做一些类似calendar.type_event ='shared_event'的事情。 But returns a 0, with NULL returns nothing. 但是返回0,带NULL不返回任何内容。 This is not the problem only I need to know if I can assign my own value: 'shared_event'. 这不是问题,仅是我需要知道是否可以分配自己的值:“ 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' . 我需要知道是否可以分配自己的值: 'shared_event'

Yes, you can. 是的你可以。 In place of column calendar.type_event just use literal 'shared_event' 代替列calendar.type_event只需使用文字'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. 在第二个查询中, type_event字段将包含空值。 If the type_event field is a numeric field, you can put 0 (ZERO) instead of '' (empty single quotes/string). 如果type_event字段是数字字段,则可以放置0 (零)而不是'' (空单引号/字符串)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在选择语句中替换/分配值 - Replace/Assign a value in select statement MySQL SELECT语句检索唯一的字段值 - MySQL SELECT statement to retrieve field value that is unique MySQL存储过程从选择语句为多个变量赋值 - MySQL store procedure Assign value to multiple variable from select statement 根据计算字段的值向 MYSQL SELECT 语句添加值 - adding a value to a MYSQL SELECT statement, depending on the value of calculated field 选择带有二进制字段的语句 - Select statement with Binary field MYSQL使用xase语句创建表作为选择,将字段转换为编号值 - MYSQL Creating table as select with xase statement converting field to numbered value MySQL select语句中的多个和,具体取决于不同字段中的布尔值 - MySQL multiple sums in select statement depending on boolean value in different field MySQL - 在同一SQL的嵌套SQL语句中使用select字段值 - MySQL - Use select field value in nested SQL statement of the same SQL 在MySQL SELECT语句中,派生字段如何利用SELECT列表中另一个字段的值? - In MySQL SELECT statement, how can a derived field utilise the value of another field in the SELECT list? 在其他select语句中选择最大值,并从嵌套的select中显示一个相关字段 - Select max value within other select statement and display also a relevant field from the nested select
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM