2013-06-12 24 views
5

Sto provando a interrogare più tabelle di WordPress e ho imparato mentre procedo.Query MySQL per creare colonne basate su altre tabelle

Ecco quello che ho finora

SELECT 
    tr.object_id, 
    tr.term_taxonomy_id, 
    p.ID, 
    p.post_date, 
    p.post_title, 
    p.post_excerpt, 
    p.guid, 
    t.term_id, 
    t.name, 
    tt.taxonomy 
FROM 
    wp_116_term_relationships AS tr, 
    wp_116_posts AS p, 
    wp_116_terms AS t LEFT JOIN 
    wp_116_term_taxonomy as tt ON tt.term_id = t.term_id 
WHERE 
    p.post_type = 'post' 
    AND p.ID = tr.object_ID 
    AND tr.term_taxonomy_id = tt.term_taxonomy_id 
    AND p.post_date > '2013-06-01' 

Ecco quello che ottengo (dispiace non riuscivo a capire come per pubblicare questo pulitore - spero che abbia senso)

object_id term_taxonomy_id ID post_date post_title post_excerpt guid term_id name taxonomy 
2356  33  2356 2013-06-07 15:56:54  Test Post for Reports  http://domain.com/?p=2356 7496 Marketing Updates category 
2356  32  2356 2013-06-07 15:56:54  Test Post for Reports  http://domain.com/?p=2356 470  News Updates category 
2356  70  2356 2013-06-07 15:56:54  Test Post for Reports  http://domain.com/?p=2356 46408 Tag Test 1  post_tag 
2356  72  2356 2013-06-07 15:56:54  Test Post for Reports  http://domain.com/?p=2356 46410 Tag Test 2  post_tag 
2356  74  2356 2013-06-07 15:56:54  Test Post for Reports  http://domain.com/?p=2356 46412 Tag Test 3  post_t 

Come faccio separare i dati dal campo del nome in modo che se è un post_tag è in una colonna (post_tag) e se è una categoria è in un altro (categoria)? Per esempio:

object_id term_taxonomy_id ID post_date post_title post_excerpt guid term_id post_tag category 
2356  33  2356 2013-06-07 15:56:54  Test Post for Reports  http://domain.com/?p=2356 7496 Marketing Updates Tag Test 1 
+0

io non sono sicuro di quello che si vuole qui ti sembra di voler fare qualcosa in base a due criteri diversi, vuoi per rilevare i post nella categoria tassonomia? –

+0

Grazie. Sì, era quello che stavo cercando di fare. Ho usato la risposta qui sotto e ha funzionato perfettamente! – user1958798

risposta

3

dovrebbe essere abbastanza semplice con una dichiarazione CASE

SELECT 
    tr.object_id, 
    tr.term_taxonomy_id, 
    p.ID, 
    p.post_date, 
    p.post_title, 
    p.post_excerpt, 
    p.guid, 
    t.term_id, 
    CASE WHEN tt.taxonomy = 'category' THEN t.name ELSE NULL END AS category_name, 
    CASE WHEN tt.taxonomy = 'post_tag' THEN t.name ELSE NULL END AS post_tag_name 
    FROM 
    wp_116_term_relationships AS tr, 
    wp_116_posts AS p, 
    wp_116_terms AS t 
    LEFT JOIN wp_116_term_taxonomy as tt ON tt.term_id = t.term_id 
    WHERE 
    p.post_type = 'post' 
    AND p.ID = tr.object_ID 
    AND tr.term_taxonomy_id = tt.term_taxonomy_id 
    AND p.post_date > '2013-06-01' 
+0

Grazie, grazie, grazie! Non ho esperienza SQL e mi sono tirato fuori i capelli cercando questo. Ha funzionato perfettamente! – user1958798

Problemi correlati