how to convert simple mysql query to eloquent laravel?
in fact I want to convert down query :
SELECT temp.* FROM
(SELECT * FROM avilableproducts ORDER BY avilableproducts.count DESC) AS temp
GROUP BY temp.products_id, temp.color
HAVING temp.products_id = 27
ORDER BY temp.count DESC;
to
$Avilableproducts = $Avilableproducts->select(DB::raw('temp.*'))
->from(DB::raw('(SELECT * FROM avilableproducts ORDER BY count DESC) AS temp'))
->groupBy(['products_id', 'color'])
->orderBy('count', 'desc');
Related
how I can do this
Ex: select 5 random rows from the 20 most recent row
I need to do this with eloquent and get data with a query from DB not with mapping data
I found this query but I can't convert this to laravel eloquent …
Query :
select tbl1.* from (select *from DemoTable ORDER BY ShippingDate DESC LIMIT 20 ) as tbl1
-> ORDER BY RAND() LIMIT 5;
you can use this code. Change Models and columns for your models and columns. I wrote a user for the test
return User::query()
->whereIn('id', function($query)
{
$query->from('users')
->selectRaw('id')
->orderByDesc('created_at')->limit(20);
})->inRandomOrder()->limit(5)->get();
if you get toSql() you see
select * from `users` where `id` in (select id from `users` order by `created_at` desc limit 20) limit 5
As this question clearly means that, I am not asking about SQL joins here,
I just want to combine/merge 2 MySQL result in PHP, i have tried to do it with PHP array_merge() but no success.
$user_paid_query = "SELECT * from users WHERE now_paid!=0 ORDER BY now_paid DESC";
$result_user_paid = $connect->query($user_paid_query);
$users_paid = $result_user_paid->fetch_assoc();
$users_unpaid_query = "SELECT * from users WHERE now_paid=0 ORDER BY id ASC";
$users = array_merge($users_paid, $users_unpaid);
You can use union in MySQL itself(instead of PHP) to merge 2 SQL results.
(SELECT * from users WHERE now_paid != 0 ORDER BY now_paid DESC)
UNION
(SELECT * from users WHERE now_paid = 0 ORDER BY id ASC)
I have records like this
here is my query
SELECT `Chat`.`id`,
`Chat`.`sender_id`,
`Chat`.`receiver_id`,
`Chat`.`message`,
`Chat`.`datetime`,
`Chat`.`converstation_id`
FROM `gopher`.`chat` AS `Chat`
WHERE ((`Chat`.`sender_id` = 10)
OR (`Chat`.`receiver_id` = 10))
GROUP BY converstation_id
ORDER BY `Chat`.`id` DESC
But here order now is not working and this is the result I am getting after running this above query
You have not used any aggregate function , so your group by is just returning 1st data set. There are several ways to fix it
Remove group by and just use order by if we you want to sort by conversation_id
Use aggregate function
SELECT `Chat`.`id`,
`Chat`.`sender_id`,
`Chat`.`receiver_id`,
`Chat`.`message`,
`Chat`.`datetime`,
`Chat`.`converstation_id`
FROM `gopher`.`chat` AS `Chat`
WHERE ((`Chat`.`sender_id` = 10) OR (`Chat`.`receiver_id` = 10))
GROUP BY converstation_id
ORDER BY `Chat`.`id` DESC LIMIT 0,1
hi i have one database and i use this query for get latest records using group by and and order by
i have use this code for get all record
SELECT id,time FROM eventlog WHERE event = '2' ORDER BY `eventlog`.`id` DESC
and i get this output:
i want to get latest time of every group
i have use this query for group by with order by query
SELECT id,time FROM eventlog WHERE event = '2' GROUP BY id ORDER BY time DESC
Try this:
SELECT id,MAX(`time`) as `time` FROM eventlog WHERE event = '2' GROUP BY id DESC
How would I do the following query in Codeigniter ActiveRecord : -
SELECT *,
(SELECT
image_path
FROM
image
WHERE
image_table = 'model'
AND image_table_id = model_id
GROUP BY image_table_id
LIMIT 1) AS ModelImg
FROM
(SELECT
*
FROM
vw_newcars
where offer_table = 'derivative'
order by offer_order
) x
WHERE make_name = 'Fiat'
group by offer_table_id
limit 12
The part I'm having problems with is how to do a select from subquery in Active Record.
I don't see a from_select function or anything comparable in the documentation.
I managed to get the query to work by putting the from sub_query into the initial select statement :
$this->db->select("*,
(select image_path from image where image_table = 'model' and image_table_id = model_id
group by image_table_id limit 1) as ModelImg FROM
(SELECT * FROM $view where offer_table = 'derivative' order by offer_order) x");