如何在特定字符后的SQL查询中拆分字符串

时间:2017-06-16 20:28:15

标签: mysql sql

我有这样的查询

SELECT wp.id,
   w.course_id,
   wt.slug,
   wp.post_title,
   w.course_id,
   w.start_date,
   wp.post_name,
   w.end_date,
   w.duration,
   w.timings,
   w.timezone,
   w.reg_link
FROM       wp_posts wp
INNER JOIN wp_terms wt
INNER JOIN wp_term_relationships tr ON wt.term_id = tr.term_taxonomy_id
INNER JOIN wp_term_taxonomy wtt     ON wt.term_id=wtt.term_id
    AND wtt.term_taxonomy_id=tr.term_taxonomy_id
    AND tr.object_id=wp.id
JOIN wp_event_list w 
    ON trim(wp.post_title) = trim(
       substring(w.course_id, position('-' IN w.course_id)+1,LENGTH (w.course_id)))
WHERE w.end_date >= CURRENT_DATE
    AND wp.post_type ='event'
    AND wtt.taxonomy = 'EventCategory'
GROUP BY w.id
ORDER BY w.start_date, w.timings ASC

w.timings的查询中,我有一个像这样的值"上午8:30 - 上午10:00和#34;。我希望显示w.timings列值,例如" 8:30 AM"。 如何在上面的查询中拆分和显示这个字符串?

我正在使用MySQL 5.6版本。

1 个答案:

答案 0 :(得分:1)

使用SUBSTRING_INDEX(str,delim,count)

它返回分隔符的#event前面的子字符串。

SUBSTRING_INDEX(w.timings, ' - ', 1) as timings

这是dbfiddle演示