2
我想整理一下我的原始查詢和我感興趣的WordPress的Meta鍵和meta值Laravel查詢生成器 - 先進的加入和選擇
如何使用Laravel查詢生成器做到這一點?
META TABLE ==
meta_id | post_id | meta_key | meta_value
1 | 101 | quantity | 8
2 | 101 | price | 100
3 | 102 | quantity | 7
4 | 102 | price | 56
5 | 103 | quantity | 12
6 | 103 | price | 256
POST TABLE ==
post_id | about
101 | Pencil | Luxurious pencil only for you
102 | Eraser | All your mistakes, gone!
103 | Pen | Unrivaled penmanship, stronger than sword.
查詢:
select
p.post_id,
p.name,
p.about,
m1.meta_value,
m2.meta_value
from post_table p
inner join meta_table m1
on m1.post_id = p.post_id and m1.meta_key = 'quantity'
inner join meta_table m2
on m2.post_id = p.post_id and m2.meta_key = 'price'
where CAST(m1.meta_value as int) < 10
order by CAST(m1.meta_value as int) asc
謝謝