2013-01-22 37 views
6

Laravel ile çalışmak için bazı PHP/MySQL yazıyorum.Tablodan akıcı bir sorgu oluşturucu ile birden çok tablo seçin

SELECT p.post_text, p.bbcode_uid, u.username, t.forum_id, t.topic_title, t.topic_time, t.topic_id, t.topic_poster 
FROM phpbb_topics t, phpbb_posts p, phpbb_users u 
WHERE t.forum_id = 9 
AND p.post_id = t.topic_first_post_id 
AND u.user_id = t.topic_poster 
ORDER BY t.topic_time 
DESC LIMIT 10 

Bu phpbb forumu sorgular ve mesajları alır: enter image description here

nasıl yeniden olabilir ben yapmak istiyorum bir şey DB with the Fluent Query Builder daha özlü sorgular ama biraz kaybetmiş olduğum yapmak olduğunu Fluent Query Builder sözdizimini kullanmak için bunu yazın? Burada

cevap

18

Test edilmedi ancak

return DB::table('phpbb_topics') 
         ->join('phpbb_posts', 'phpbb_topics.topic_first_post_id', '=', 'phpbb_posts.post_id') 
         ->join('phpbb_users', 'phpbb_topics.topic_poster', '=', 'phpbb_users.user_id') 
         ->order_by('topic_time', 'desc') 
         ->take(10) 
         ->get(array(

            'post_text', 
            'bbcode_uid', 
            'username', 
            'forum_id', 
            'topic_title', 
            'topic_time', 
            'topic_id', 
            'topic_poster' 


         )); 
+0

Ah farkında değildi Eğer zincir katılır olabilir. Faydalı, teşekkürler. - - –

+0

(5.3 test) altına gibi bir seçenek yoktur> (get array ('phpbb_topics *', 'kullanıcı adı' İngilizce konuşan değil için, 'phpbb_posts.post_text' ) – Sadat

2

bu kodu deneyebilirsiniz bir başlangıçtır. İhtiyacınız olanı tamamlamanız gerekir.

DB::select(DB::raw("SELECT p.post_text, p.bbcode_uid, u.username, t.forum_id, t.topic_title, t.topic_time, t.topic_id, t.topic_poster 
FROM phpbb_topics t, phpbb_posts p, phpbb_users u 
WHERE t.forum_id = 9 
AND p.post_id = t.topic_first_post_id 
AND u.user_id = t.topic_poster 
ORDER BY t.topic_time 
DESC LIMIT 10")); 
+1

, sen teşhis gibiydi sorun iyi: P – rayryeng

1
return DB::table(DB::raw('phpbb_topics t, phpbb_posts p, phpbb_users u')) -> select(DB::raw('p.post_text, p.bbcode_uid, u.username, t.forum_id, t.topic_title, t.topic_time, t.topic_id, t.topic_poster'))->where('phpbb_topics.topic_first_post_id', '=', 'phpbb_posts.post_id')->where('phpbb_users', 'phpbb_topics.topic_poster', '=', 'phpbb_users.user_id')->order_by('topic_time', 'desc')->take(10)->get(); 
İlgili konular