Yêu cầu thg 8 16, 2021 3:32 SA 163 0 1
  • 163 0 1
-1

Ai giúp mình chuyển câu sql này qua laravel với

Chia sẻ
  • 163 0 1
SELECT 
    COUNT(b.book_id) AS number_book,
    b.language,
    SUM(b.single) AS single_number,
    SUM(b.couple) AS couple_number
FROM
    (SELECT 
        b.book_id,
        b.language,
		ROUND(SUM(CASE WHEN plt.depend_flag = 1 THEN .5 ELSE 0 END)) AS couple,
		SUM(CASE WHEN plt.depend_flag = 0 THEN 1 ELSE 0 END) AS single
    FROM book AS b
    INNER JOIN plan AS pl ON b.book_id = pl.book_id
    LEFT JOIN plan_type AS plt ON pl.plan_type_id = plt.plan_type_id
    INNER JOIN customer c ON pl.plan_id = c.plan_id
    WHERE
        b.status = 1 
        AND b.book_type IN (1 , 3)
		AND b.create_time >= '2020-08-04 00:00:00'
		AND b.create_time <= '2020-08-04 23:59:59'
    GROUP BY b.book_id) b
GROUP BY b.language

1 CÂU TRẢ LỜI


Đã trả lời thg 8 16, 2021 3:38 SA
+6
DB::raw("SELECT COUNT(b.book_id) AS number_book, b.language, SUM(b.single) AS single_number, SUM(b.couple) AS couple_number FROM (SELECT b.book_id, b.language, ROUND(SUM(CASE WHEN plt.depend_flag = 1 THEN .5 ELSE 0 END)) AS couple, SUM(CASE WHEN plt.depend_flag = 0 THEN 1 ELSE 0 END) AS single FROM book AS b INNER JOIN plan AS pl ON b.book_id = pl.book_id LEFT JOIN plan_type AS plt ON pl.plan_type_id = plt.plan_type_id INNER JOIN customer c ON pl.plan_id = c.plan_id WHERE b.status = 1 AND b.book_type IN (1 , 3) AND b.create_time >= '2020-08-04 00:00:00' AND b.create_time <= '2020-08-04 23:59:59' GROUP BY b.book_id) b GROUP BY b.language");
Chia sẻ
Avatar LongThanh @LongThanh.it
thg 8 23, 2021 10:58 SA

Haha, ngắn gọn súc tích 😃

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí