Video
-------
id
source_low
source_med
source_high
Source
--------
id
duration
thumbnail
L'elemento video può contenere almeno 1 o al massimo 3 della sorgente (sorgente di bassa, fonte med, sorgente ad alta).Ottenere il valore nella tabella che qualche colonna è nullo in MYSQL
L'idea è la stessa di YouTube, il che significa che un record video può contenere diverse qualità. Quindi, quando restituisco la durata/anteprima vorrei verificare quale fonte esiste e restituirne una.
Ad esempio, se un video ha source_low o source_med, significa che c'è la miniatura & per source_low e source_med, quindi devo solo restituirne uno.
Codeigniter sintassi:
$this->db->select('v.*, sl.duration, sm.duration as duration_m, sh.duration as duration_h, sl.thumbnail, sm.thumbnail as thumbnail_m, sh.thumbnail as thumbnail_h');
$this->db->from('video as v');
$this->db->join('source as sl', 'v.source_low = sl.video_id', 'left');
$this->db->join('source as sm', 'v.source_med = sm.video_id', 'left');
$this->db->join('source as sh', 'v.source_high = sh.video_id', 'left');
$this->db->group_by('v.id');
SQL (miniature è allo stesso modo da ottenere):
SELECT v.*, sl.duration, sm.duration as duration_sm, sh.duration as duration_sh
FROM video as v
LEFT JOIN source as sl ON (v.source_low = sl.video_id)
LEFT JOIN source as sm ON (v.source_med = sm.video_id)
LEFT JOIN source as sh ON (v.source_high = sh.video_id)
GROUP BY v.id;
In questo momento posso restituire tutto il risultato, compreso il valore nullo, ma come posso restituisce solo una durata e una miniatura?
Grazie per l'aiuto
Aggiornamento:
solo aggiornare la query, ma sembra CodeIgniter ha qualche errore di gestirlo, qualche idea? Grazie
Il mio codice:
$this->db->select('v.*, COALESCE(b.duration, bt.duration, bc.duration) AS duration, COALESCE(b.thumbnail, bt.thumbnail, bc.thumbnail) AS thumbnail');
E in SQL:
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS duration, COALESCE(b.thumbnail, `bt`.`thumbnail`, `bc`.`thumbnail)` AS thumbn' at line 1
SELECT `v`.*, `t`.`name`, `t`.`name_tw`, `t`.`name_cn`, COALESCE(b.duration, `bt`.`duration`, `bc`.`duration)` AS duration, COALESCE(b.thumbnail, `bt`.`thumbnail`, `bc`.`thumbnail)` AS thumbnail FROM (`video` as v) JOIN `teacher` as t ON `v`.`teacher_id` = `t`.`id` LEFT JOIN `brightcove` as b ON `v`.`video` = `b`.`video_id` LEFT JOIN `brightcove` as bt ON `v`.`video_tw` = `bt`.`video_id` LEFT JOIN `brightcove` as bc ON `v`.`video_cn` = `bc`.`video_id` WHERE `v`.`is_delete` = 0 AND `v`.`type` = 0 GROUP BY `v`.`id` ORDER BY `v`.`start_date` desc
Questa è una buona domanda, ma è difficile per eseguire il debug di SQL perché è contenuta all'interno di tutte quelle irrilevanti 'stringhe php'. Sbarazzati di quelli e mostraci l'SQL e saremo in grado di aiutarti! – LondonRob
Grazie. scusa per la confusione, è la query codeigniter e la converto in SQL uno – user782104
messaggio di errore che hai aggiunto non corrisponde al codice che hai lì. – vhu