0

必要なものに近いが正確ではないクエリがあります。

SELECT DISTINCT p.id, 
                Ifnull(p.last_name, '--')         last_name, 
                Ifnull(p.first_name, '--')        first_name, 
                Ifnull(p.city, '--')              city, 
                Ifnull(p.state, '--')             state, 
                Ifnull(e.full_name, '--')         full_name, 
                Ifnull(o.current_step, '--')      current_step, 
                Ifnull(o.current_step_date, '--') current_step_date
FROM   prospect AS p 
       JOIN opportunity AS o 
         ON o.prospect_id = p.id 
       JOIN employee AS e 
         ON p.id_ofproducer = e.id 
WHERE  p.id = 1234 

Pから行を取り戻すことを望んでいます

注: o または e レコードが複数ある場合は MAX ID を使用し、ない場合は「--」を使用します。

4

2 に答える 2

1

left joinレコードがない場合を処理するにはが必要です。oそして、とeレコードの最大値を見つける必要があります。e実際には、ID で参加しているため、レコードに問題はないはずです。クエリを作成する 1 つの方法を次に示します。

SELECT DISTINCT p.id, 
                Ifnull(p.last_name, '--')         last_name, 
                Ifnull(p.first_name, '--')        first_name, 
                Ifnull(p.city, '--')              city, 
                Ifnull(p.state, '--')             state, 
                Ifnull(e.full_name, '--')         full_name, 
                Ifnull(o.current_step, '--')      current_step, 
                Ifnull(o.current_step_date, '--') current_step_date
FROM   prospect AS p 
       left JOIN opportunity AS o 
         ON o.prospect_id = p.id and
            o.id = (select id from opportunity o2 where o2.prospect_id = p.id order by id desc limit 1)
       left JOIN employee AS e 
         ON p.id_ofproducer = e.id
WHERE  p.id = 1234 
于 2013-06-12T20:35:35.063 に答える
0
SELECT p.id, 
       Ifnull(p.last_name, '--')         last_name, 
       Ifnull(p.first_name, '--')        first_name, 
       Ifnull(p.city, '--')              city, 
       Ifnull(p.state, '--')             state, 
       Ifnull(e.full_name, '--')         full_name, 
       Ifnull(o.current_step, '--')      current_step, 
       Ifnull(o.current_step_date, '--') current_step_date
FROM   prospect AS p 
       JOIN opportunity AS o 
         ON o.prospect_id = p.id 
       JOIN employee AS e 
         ON p.id_ofproducer = e.id 
WHERE  p.id = 1234
ORDER BY o.prospect_id DESC, e.id DESC
LIMIT 1
于 2013-06-12T20:22:39.013 に答える