Php 如何使用查询生成器Laravel构建包含联接查询和子查询的复杂查询
晚上好,我的程序员朋友们 正如这个问题的标题所说,我有一个有点复杂的mysql查询,我正在尝试使用Laravel中的query builder来写它,但是每次我尝试写它时,当到达子查询时,我就停止了。我将写下查询,并让您知道查询将做什么Php 如何使用查询生成器Laravel构建包含联接查询和子查询的复杂查询,php,mysql,laravel,Php,Mysql,Laravel,晚上好,我的程序员朋友们 正如这个问题的标题所说,我有一个有点复杂的mysql查询,我正在尝试使用Laravel中的query builder来写它,但是每次我尝试写它时,当到达子查询时,我就停止了。我将写下查询,并让您知道查询将做什么 SELECT brands.name_en as brand_name_en, brands.name_ar as brand_name_ar, brands.brand_url as brand_url, brands.
SELECT
brands.name_en as brand_name_en,
brands.name_ar as brand_name_ar,
brands.brand_url as brand_url,
brands.description_ar as description_ar,
brands.description_en as description_en,
categories.name_en as category_name_en,
categories.name_ar as category_name_ar
FROM
brands
INNER JOIN categories ON brands.category_id = categories.id
WHERE
(brands.status = "1")
AND
(
brands.id IN (
SELECT
distinct(items.brand_id)
FROM
items
WHERE
items.in_stock = "1"
GROUP BY
(items.brand_id)
)
)
查询说明:
我有三张桌子:
- 品牌(标识、名称、类别\u标识)
- 类别(标识、名称、品牌标识)
- 项目(标识、品牌标识、库存、销售日期)
Brands Table
------------
id name category_id
1 googleplay 1
Categories Table
--------------------------
id name brand_id
1 cards 1
items Table
-----------
id brand_id in_stock selling_date
1 1 0 null
1 1 1 2017-02-02 04:04:49
已经尝试了将近2天,但没有运气,因为当它到达子查询部分时,它真的很复杂
真的希望能从你们这里得到帮助。如果你们需要更多的解释请告诉我,我可能在写问题的时候忘了什么
提前感谢您。如果您稍微更改一下sql,您可以使用内部联接:
SELECT brands.name, categories.name
FROM brands
INNER JOIN categories ON brands.category_id = categories.id
INNER JOIN items ON categories.id = items.brand_id
WHERE brands.status = 1 AND items.in_stock = 1
将此转换为Laravel很简单:
\DB::table('brands')
->join('categories', 'brands.category_id', '=', 'categories.id')
->join('items', 'brands.id', '=', 'items.brand_id')
->select('brands.name as brand', 'categories.name as category')
->where('brands.status', 1)
->where('items.in_stock', 1)
->get();
如果稍微更改sql,可以使用内部联接:
SELECT brands.name, categories.name
FROM brands
INNER JOIN categories ON brands.category_id = categories.id
INNER JOIN items ON categories.id = items.brand_id
WHERE brands.status = 1 AND items.in_stock = 1
将此转换为Laravel很简单:
\DB::table('brands')
->join('categories', 'brands.category_id', '=', 'categories.id')
->join('items', 'brands.id', '=', 'items.brand_id')
->select('brands.name as brand', 'categories.name as category')
->where('brands.status', 1)
->where('items.in_stock', 1)
->get();
从概念上讲,我们从上个月的项目表中选择项目,对它们进行排序,然后通过连接获取品牌和类别 它可以在单个查询中写入,如下所示:
DB::table('items')
->join('brands', 'items.brand_id', '=', 'brands.id')
->join('categories', 'brands.category_id', '=', 'categories.id')
->select('brands.name as brand', 'categories.name as category')
->where('brands.status', 1) // as per your query
->whereRaw(YEAR(items.selling_date) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)) // year condition
->whereRaw(MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)) // last month condition
->orderBy('items.in_stock', 'desc') // orderBy the sales
->distinct() // Since you only require brand names and category names, choosing distinct will avoid the need of the GROUP BY eliminating the multiple occurances of a brand in the items table
->get()
我还没有尝试运行此查询,但希望它能有所帮助。如果我误解了这个问题,请随时纠正我。从概念上讲,我们从上个月的项目表中选择项目,对其进行排序,然后通过连接获取品牌和类别 它可以在单个查询中写入,如下所示:
DB::table('items')
->join('brands', 'items.brand_id', '=', 'brands.id')
->join('categories', 'brands.category_id', '=', 'categories.id')
->select('brands.name as brand', 'categories.name as category')
->where('brands.status', 1) // as per your query
->whereRaw(YEAR(items.selling_date) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)) // year condition
->whereRaw(MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)) // last month condition
->orderBy('items.in_stock', 'desc') // orderBy the sales
->distinct() // Since you only require brand names and category names, choosing distinct will avoid the need of the GROUP BY eliminating the multiple occurances of a brand in the items table
->get()
我还没有尝试运行此查询,但希望它能有所帮助。如果我误解了这个问题,请随时纠正。也许可以使用
whereInSub
?还有,你能展示一下你迄今为止雄辩的问题,以及你陷入其中的原因吗?@uruloke当然,给我一分钟,我正在寻找贾维先生给出的答案。老实说,在我搜索文档的过程中,我没有看到whereInSup
这是我第一次知道它的存在。请看,您可以直接使用where
。如果第二个参数是闭包,它会在下面使用whereInSub
。@uruloke是的,我想使用where将是解决查询的方法,但我不知道在哪里使用它,因为代码变得疯狂,查询很复杂。可能使用whereInSub
?还有,你能展示一下你迄今为止雄辩的问题,以及你陷入其中的原因吗?@uruloke当然,给我一分钟,我正在寻找贾维先生给出的答案。老实说,在我搜索文档的过程中,我没有看到whereInSup
这是我第一次知道它的存在。请看,您可以直接使用where
。如果第二个参数是闭包,它会在下面使用whereInSub
。@uruloke是的,我想使用where将是解决查询的方法,但我不知道在哪里使用它,因为代码变得疯狂,查询很复杂。谢谢你,Lav,我正在调整你的代码以匹配我想要的数据,会让你知道发生了什么。我也在检查Javi的答案。我还在调整,因为我没有得到正确的结果,所以在查询中有点困惑。但是我确信你的代码与我需要的非常接近你的答案与我想要的非常接近,查询非常复杂,因此我不得不在时间部分根据日期对数据进行排序。说到这里,是的,你的答案很接近。谢谢谢谢你,拉夫,我正在调整你的代码以匹配我想要的数据,会让你知道发生了什么。我也在检查Javi的答案。我还在调整,因为我没有得到正确的结果,所以在查询中有点困惑。但是我确信你的代码与我需要的非常接近你的答案与我想要的非常接近,查询非常复杂,因此我不得不在时间部分根据日期对数据进行排序。说到这里,是的,你的答案很接近。非常感谢。