$query = "SELECT *, (id.number_of_candidates - t.counted) as available
FROM interview_dates id
LEFT JOIN (SELECT interview_dates_id,COUNT(interview_dates_id) as counted
FROM date_timeslot GROUP BY interview_dates_id) as t
ON t.interview_dates_id=id.id
WHERE id.number_of_candidates > t.counted";
Please help me to rewrite this using Laravel Query Builder.
Related
How can I make this query in Laravel:
SELECT * FROM posts
LEFT OUTER JOIN (SELECT * from subscribers where subscribers.post_id=2)
as sub ON sub.university_id = universities.id
try this, i get that from sql to laravel query builder converter online
DB::table('posts')
->leftJoin('(SELECT * from subscribers where subscribers.post_id=2) as
sub','sub.university_id','=','universities.id')
->get();
Hi I my try to convert SQL query to Query Builder. My Sql query below like this
SELECT exclusive_distributor_opens.distribution_code,exclusive_distributor_opens.distribution_name,exclusive_item_opens.item_code,exclusive_item_opens.item_name,SUM(exclusive_budget_items_details.dealer_contribution) as Total,exclusive_distributor_opens.date
FROM exclusive_budget_masters
JOIN exclusive_distributor_opens
ON exclusive_budget_masters.distributor_id=exclusive_distributor_opens.id
JOIN exclusive_budget_items_details
ON exclusive_budget_masters.distributor_id=exclusive_budget_items_details.budget_master_id
JOIN exclusive_item_opens
ON exclusive_budget_items_details.item_id=exclusive_item_opens.id
GROUP BY (exclusive_distributor_opens.distribution_code)
HAVING exclusive_distributor_opens.date BETWEEN '2018-12-19' AND '2018-12-19'
ORDER BY exclusive_distributor_opens.distribution_code DESC
2.My Query Builder Query like this
$results = DB::table('exclusive_budget_masters')
->join('exclusive_distributor_opens', 'exclusive_budget_masters.distributor_id', '=', 'exclusive_distributor_opens.id')
->join('exclusive_budget_items_details', 'exclusive_budget_masters.distributor_id' ,'=', 'exclusive_budget_items_details.budget_master_id')
->join('exclusive_item_opens', 'exclusive_budget_items_details.item_id', '=', 'exclusive_item_opens.id')
->select('exclusive_distributor_opens.distribution_code', 'exclusive_distributor_opens.distribution_name','exclusive_budget_items_details.customar_contribution', DB::raw('SUM(exclusive_budget_items_details.customar_contribution) As Total'))
->whereBetween('exclusive_distributor_opens.date', array('2018-12-19', '2018-12-19'))
->groupBy('exclusive_distributor_opens.distribution_code', 'exclusive_distributor_opens.distribution_name','exclusive_budget_items_details.customar_contribution')
->orderBy('exclusive_distributor_opens.date', 'DESC')
->get();
My Query Output Result Comes like this:
But My Expected Result looks like this:
Please anybody help me.
Why do you expect this to work?
Select part is missing or wrong:
exclusive_item_opens.item_code missing
exclusive_item_opens.item_name missing
exclusive_distributor_opens.date missing
SUM Total column is wrong:
SUM(exclusive_budget_items_details.dealer_contribution) as Total vs
DB::raw('SUM(exclusive_budget_items_details.customar_contribution) As Total')
Your builder query looks like this:
SELECT `exclusive_distributor_opens`.`distribution_code`,
`exclusive_distributor_opens`.`distribution_name`,
`exclusive_budget_items_details`.`customar_contribution`,
Sum(exclusive_budget_items_details.customar_contribution) AS Total
FROM `exclusive_budget_masters`
INNER JOIN `exclusive_distributor_opens`
ON `exclusive_budget_masters`.`distributor_id` =
`exclusive_distributor_opens`.`id`
INNER JOIN `exclusive_budget_items_details`
ON `exclusive_budget_masters`.`distributor_id` =
`exclusive_budget_items_details`.`budget_master_id`
INNER JOIN `exclusive_item_opens`
ON `exclusive_budget_items_details`.`item_id` =
`exclusive_item_opens`.`id`
WHERE `exclusive_distributor_opens`.`date` BETWEEN ? AND ?
GROUP BY `exclusive_distributor_opens`.`distribution_code`,
`exclusive_distributor_opens`.`distribution_name`,
`exclusive_budget_items_details`.`customar_contribution`
ORDER BY `exclusive_distributor_opens`.`date` DESC
How would I use query builder in Laravel to generate the following SQL statement:
SELECT MAX(QTE) FROM (SELECT SUM(activity_sale_report.quantity_sold)
AS QTE FROM activity_sale_report
GROUP BY activity_sale_report.activity_id) AS T
If you are just using the query builder and not models you can use raw queries
$query = DB::select( DB::raw("SELECT MAX(QTE) FROM (SELECT SUM(activity_sale_report.quantity_sold) AS QTE FROM activity_sale_report GROUP BY activity_sale_report.activity_id) AS T);
I would recommend using the eloquent models, it's easier.
I have the following MySQL query which fetches a list of the last 9 authors to write a post and lists them in order of the date of the last post they wrote.
It's working properly but I'd like to re-write it using the Laravel Query Builder. Here is the query at the moment:
$authors = DB::select("
SELECT
`a`.`id`,
`a`.`name`,
`a`.`avatar`,
`a`.`slug` AS `author_slug`,
`p`.`subheading`,
`p`.`title`,
`p`.`slug` AS `post_slug`,
`p`.`summary`,
`p`.`published_at`
FROM
`authors` AS `a`
JOIN
`posts` AS `p`
ON `p`.`id` =
(
SELECT `p2`.`id`
FROM `posts` AS `p2`
WHERE `p2`.`author_id` = `a`.`id`
ORDER BY `p2`.`published_at` DESC
LIMIT 1
)
WHERE
`a`.`online` = 1
ORDER BY
`published_at` DESC
LIMIT 9
");
I understand the basics of using the query builder, but there doesn't appear to be anything in the Laravel docs that allows for me to JOIN a table ON a SELECT.
Can anyone suggest a way that I can write this query using the Laravel Query builder, or perhaps suggest a way that I can rewrite this query to make it easier to structure with the query builder?
Try to do like this
$data = DB::table('authors')
->select(
'a.id',
'a.name',
'a.avatar',
'a.slug AS author_slug',
'p.subheading',
'p.title',
'p.slug AS post_slug',
'p.summary',
p.published_at')
->from('authors AS a')
->join('posts AS p', 'p.id', '=', DB::raw("
(
SELECT p2.id FROM posts AS p2
WHERE p2.author_id = b.id
ORDER BY p2.published_at
DESC LIMIT 1
)"))
->where('a.online', 1)
->limit(9)
->orderBy('p.published_at', 'desc')
->get();
here is my query and this give proper ouput.
But How can I set it according to magento rules?
SELECT main_table.*,
(select company from sales_flat_order_address sfoa where sfoa.entity_id=sfo.billing_address_id) as bill_to_company,
(select company from sales_flat_order_address sfoa where sfoa.entity_id=sfo.shipping_address_id) as ship_to_company
FROM sales_flat_order_grid as main_table inner join sales_flat_order as sfo
on main_table.entity_id = sfo.entity_id
please help me....
Thanks & Reagards
Praful
You can use magento default connection Model
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$result=$write->query("your query as like mysql query no different");
$row = $readresult->fetch(); // this will fetch all your data