我想在mysql中的concat()中格式化数据。但是当我尝试这样做时,mysql禁止我。这是功能:
SELECT user_id as id, DATE(leave_from) as date_from, DATE(leave_to) as date_to, NULL as delayed_checkin_remarks, NULL as early_checkout_remarks,
CONCAT(
(' LeaveType : ',
SELECT title FROM leave_type
WHERE id = l.leave_type_id),':<br/>',leave_description)
as leave_remarks, leave_status
FROM users_leave_request l
WHERE DATE(leave_from) between '2015-01-26' AND '2015-01-30'
问题是当我把那个&#39; LeaveType:&#39;在SELECT内部进行CONCAT之前..否则它工作正常。我该如何纠正?
答案 0 :(得分:2)
CONCAT(
这样更好:
SELECT user_id as id, DATE(leave_from) as date_from, DATE(leave_to) as date_to, NULL as delayed_checkin_remarks, NULL as early_checkout_remarks,
CONCAT(
' LeaveType : ',
(SELECT title FROM leave_type
WHERE id = l.leave_type_id),':<br/>',leave_description)
as leave_remarks, leave_status
FROM users_leave_request l
WHERE DATE(leave_from) between '2015-01-26' AND '2015-01-30'
但是,不是使用嵌套查询来获取leave_type描述,而是在主查询中的leave_type表上执行JOIN:
SELECT l.user_id as id, DATE(l.leave_from) as date_from, DATE(l.leave_to) as l.date_to, NULL as delayed_checkin_remarks, NULL as early_checkout_remarks,
CONCAT(' LeaveType : ', lt.title,':<br/>',l.leave_description) AS leave_remarks,
l.leave_status
FROM users_leave_request l
INNER JOIN leave_type lt ON lt.id=l.l.leave_type_id
WHERE DATE(l.leave_from) between '2015-01-26' AND '2015-01-30'
答案 1 :(得分:0)
您还必须在Select语句周围使用引号。像这样的东西 -
SELECT user_id as id, DATE(leave_from) as date_from, DATE(leave_to) as date_to, NULL as delayed_checkin_remarks, NULL as early_checkout_remarks,
CONCAT(
(' LeaveType : ',
'SELECT title FROM leave_type
WHERE id = l.leave_type_id)',':<br/>',leave_description)
as leave_remarks, leave_status
FROM users_leave_request l
WHERE DATE(leave_from) between '2015-01-26' AND '2015-01-30'