Php Laravel查询生成器-高级联接和选择
我试图整理我的原始查询,我对wordpress的元键和元值感兴趣 如何使用Laravel查询生成器执行此操作Php Laravel查询生成器-高级联接和选择,php,mysql,sql,laravel,Php,Mysql,Sql,Laravel,我试图整理我的原始查询,我对wordpress的元键和元值感兴趣 如何使用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
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
选择
p、 邮政编码,
p、 名字,
p、 关于,,
m1.meta_值,
m2.meta_值
从post_表p
内部联接元表m1
在m1.post_id=p.post_id和m1.meta_key='quantity'
内部联接元表m2
在m2.post_id=p.post_id和m2.meta_key='price'
其中,强制转换(m1.meta_值为int)<10
按转换顺序(m1.meta_值为int)asc
谢谢我在的帮助下构建了下面的查询,希望这对您有用。我没有接受过这个测试
DB::table('post_table AS p')
->select('p.post_id', 'p.name', 'p.about', 'meta_value', 'meta_value')
->join('meta_table AS m1','m1.post_id','=','p.post_id')
->where('m1.meta_key', 'quantity')
->join('meta_table AS m2','m2.post_id','=','p.post_id')
->where('m2.meta_key', 'price')
->where(DB::raw('CAST(m1.meta_value AS INT)','<',10)
->orderBy(DB::raw('CAST(m1.meta_value AS INT)'), 'asc')
->get();
DB::table('post_table AS p')
->选择('p.post\u id','p.name','p.about','meta\u value','meta\u value')
->join('meta_表为m1','m1.post_id','=','p.post_id'))
->式中('m1.meta_键','数量')
->join('meta_table AS m2','m2.post_id','=','p.post_id'))
->其中('m2.meta_键'、'price')
->其中(DB::raw('CAST(m1.meta_值为INT)'