php
  • mysql
  • sql
  • 2011-12-02 103 views 0 likes 
    0

    如何讓這兩行的結果,兩列 我的疑問是 -如何設置查詢以獲得如下結果?

    select tt.* from wp_terms AS tt where tt.terms_id='" . $establishment['term_id'] . "' AND tt.meta_key ='phone'; 
    select tt.* from wp_terms AS tt where tt.terms_id='" . $establishment['term_id'] . "' AND tt.meta_key ='location'; 
    

    每個查詢給我造成的

    meta_id terms_id meta_key meta_value 
    6926  16   phone  0000 
    
    meta_id terms_id meta_key meta_value 
    6927 16   location xxx 
    

    現在我想作爲導致

    meta_id terms_id meta_value  meta_value1 
    6926  16  0000   xxx 
    

    請幫忙 謝謝

    +0

    你問我們如何獲取2行與一個查詢? –

    回答

    0
    SELECT t.meta_id, t.terms_id, t.meta_value, tt.metaValue AS meta_value1 
    FROM wp_terms AS t 
    INNER JOIN wp_terms AS tt ON t.terms_id = tt.terms_id 
        AND t.meta_key = 'phone' 
        AND tt.meta_key = 'location' 
    WHERE t.terms_id='" . $establishment['term_id'] . "' 
    
    +0

    ,其優秀的感謝之手 – Sparkx

    0

    也許就是這樣。

    select phone.meta_id, phone.terms_id, phone.meta_value as phone, location.meta_value as location 
    from 
    (select tt.* from wp_terms AS tt where tt.terms_id='" . $establishment['term_id'] . "' AND tt.meta_key ='phone') phone, 
    (select tt.* from wp_terms AS tt where tt.terms_id='" . $establishment['term_id'] . "' AND tt.meta_key ='location') location 
    where phone.meta_id = location.meta_id 
    
    相關問題