Get 2 Previous and Next with Current entry from database in laravel - mysql

I'm showing the image on a page. I am getting data with this
$designs = Design::take(10)->where('show', '1')->where('id', $id)->orderBy('hit', 'desc')->get();
Now I want 2 previous and 2 next records so users can select and see them.
Something like
Select 5 entry where the middle should be 'id'

The solution I came up with is the following:
You find the middle design.
Using it's id, you can get the previous 2 and make an union with a similar query that gets the next 2 (along with the original).
$design = Design::select('id')
->where('name', 'Sunset')
->firstOrFail();
$designs = Design::query()
->where('id', '<', $design->id)
->orderByDesc('id')
->limit(2)
->union(
Design::query()
->where('id', '>=', $design->id)
->orderBy('id')
->limit(3)
)
->orderBy('id')
->get();
The SQLs generated by this query are the following:
SELECT `id` FROM `designs`
WHERE `name` = "Sunset"
LIMIT 1
(
SELECT * FROM `designs`
WHERE `id` < ?
ORDER BY `id` DESC
LIMIT 2
)
UNION
(
SELECT * FROM `designs`
WHERE `id` >= ?
ORDER BY `id` ASC
LIMIT 3
)
ORDER BY `id` ASC
... Or if you'd rather have the results more separated
$design = Design::where('name', 'Sunset-Image')->firstOrFail();
$previous = Design::where('id', '<', $design->id)->orderByDesc('id')->limit(2)->get();
$next = Design::where('id', '>', $design->id)->orderBy('id')->limit(2)->get();

Related

Order query by subquery. Eloquent vs MySql

I would like to get lists which are followed by a particular user and order lists by created_at(by date when lists were followed). Not sure why it doesn't work (order by) using Eloquent:
$myQuery = PlaceList::query()
->where('private', 0)
->whereHas('followers', function ($query) use ($userID) {
$query->where('created_by_user_id', $userID);
})
->orderByDesc(
Follow::query()->select('created_at')
->where('created_by_user_id', $userID)
->where('followable_id', 'place_lists.id')
)
->get()
This is what I've got when I use dd($myQuery->toSql()):
select
*
from
`place_lists`
where
`private` = ?
and exists (
select
*
from
`follows`
where
`place_lists`.`id` = `follows`.`followable_id`
and `created_by_user_id` = ?
and `follows`.`deleted_at` is null
)
and `place_lists`.`deleted_at` is null
order by (
select
`created_at`
from
`follows`
where
`created_by_user_id` = ?
and `followable_id` = ?
and `follows`.`deleted_at` is null
) desc
But when I populate MySql with data it does work:
select
*
from
`place_lists`
where
`private` = 0
and exists (
select
*
from
`follows`
where
`place_lists`.`id` = `follows`.`followable_id`
and `created_by_user_id` = 13
and `follows`.`deleted_at` is null
)
and `place_lists`.`deleted_at` is null
order by (
select
`created_at`
from
`follows`
where
`created_by_user_id` = 13
and `followable_id` = `place_lists`.`id`
and `follows`.`deleted_at` is null
) desc
I'm well confused, what I'm doing wrong?
This query, you assert that followalbe_id is equal to 'place_lists.id'.
->orderByDesc(
Follow::query()->select('created_at')
->where('created_by_user_id', $userID)
->where('followable_id', 'place_lists.id')
)
To compare two columns use whereColumn() instead.
->orderByDesc(
Follow::query()->select('created_at')
->where('created_by_user_id', $userID)
->whereColumn('followable_id', 'place_lists.id')
)
I'm not sure if I'm on the right track here but in line 9 of your eloquent builder the query generated will use a string to compare followable_id against and therefore look like this:
and `followable_id` = 'place_lists.id'
Using DB::raw() in your where-clause in line 9 should fix that:
$myQuery = PlaceList::query()
->where('private', 0)
->whereHas('followers', function ($query) use ($userID) {
$query->where('created_by_user_id', $userID);
})
->orderByDesc(
Follow::query()->select('created_at')
->where('created_by_user_id', $userID)
->where('followable_id', DB::raw('`place_lists`.`id`')) // here
)
->get()

1242 Subquery returns more than 1 row

I got this error, I hope you may help me. I want to show a certain item in a search.
SELECT p.id, p.property_rank, p.pic_numb, p.att_numb, p.confirm, p.finalized ,p.deleted, p.user_id, p.add_date, p.visit_time,p.visit_date,p.sent_numb, p.contact_numb, zip_name, zip_id, p.street, p.sp_featured, p.property_title, p.b_price_unit, p.b_price_si, p.b_price, p.b_price, p.street_no, p.field_54,
p.field_409,
( SELECT `listing_type`.`id`
FROM `res_rpl_listing_types` AS `listing_type`
WHERE `listing_type`.`id` = (
SELECT `listing`.`type`
FROM `res_rpl_listings` AS `listing`
WHERE `listing`.`id` = p.`listing`)
) AS `listing_type_id`,
p.listing, p.googlemap_ln, p.googlemap_lt, p.category, p.b_bedrooms, p.b_bathrooms, p.sp_openhouse, p.b_price_period, p.b_lot_area_unit, p.b_lot_area_si, p.b_lot_area, p.b_lot_area, p.b_living_area_unit, p.b_living_area_si, p.b_living_area, p.b_living_area, p.description, p.sp_hot, p.sp_forclosure
FROM res_rpl_properties AS p
WHERE 1 AND p.`type` = '0' AND p.`confirm` = '1' AND p.`finalized` = '1' AND p.`deleted` = '0' AND p.`category` IN(9,8,10)
ORDER BY p.add_date DESC
LIMIT 0 , 12
The error is telling you that your subquery (selected as listing_type_id) returns more than one row. To rephrase - it's returning more than one value for listing_type_id. You should limit the results from the subquery to just one.
You have two options:
OR select just the first row of subquery
(SELECT `listing_type`.`id`
FROM `res_rpl_listing_types` AS `listing_type`
WHERE `listing_type`.`id` = (
SELECT `listing`.`type`
FROM `res_rpl_listings` AS `listing`
WHERE `listing`.`id` = p.`listing`
LIMIT 1
)
LIMIT 1
) AS `listing_type_id`
OR use IN to allow multiple comparation
(SELECT `listing_type`.`id`
FROM `res_rpl_listing_types` AS `listing_type`
WHERE `listing_type`.`id` IN (
SELECT `listing`.`type`
FROM `res_rpl_listings` AS `listing`
WHERE `listing`.`id` = p.`listing`)
LIMIT 1
) AS `listing_type_id`
The problem is there is 2 subqueries and you need to treat both. Both of them need to be limited to 1 row only.

Get last X rows using LIMIT and ORDER BY col ASC

This query is to retrieve some messages, it retuns all of them:
$q = "
SELECT *
FROM pms
WHERE
(
(id_to = $id and id_from = ".sesion().")
OR
(id_from = $id and id_to = ".sesion().")
)
AND (id > $from)
ORDER by fecha ASC";
The thing is i would like to get the last 50 elements, but I think its ony posible using DESC ordering..
how can i do it?
Do i need to count first how many rows so then can I use LIMIT $many-$ipp,$many ? or is there a way to invert the result order?
Just make your query a subquery:
SELECT * FROM
(
SELECT *
FROM pms
WHERE
(
(id_to = $id AND id_from = ".sesion().")
OR (id_from = $id and id_to = ".sesion().")
)
AND id > $from
ORDER BY fecha DESC
LIMIT 50
) q1
ORDER BY fecha ASC

select last row only if a where condition is met

I have a column for messages and I am trying to figure out how I should display messages in "sent" folder.
I run the following query:
SELECT
`text`,
`created`
FROM
`messages`
WHERE
`status` = 1
ORDER BY `created` DESC
LIMIT 1
I want to introduce a condition so that result is returned only when the last row with status = 1 also has user_from = $user (If the last row has user_to = $user, then nothing should be returned).
What should my query be?
you can use a subquery
select `text`, `created` from T1
( SELECT
`text`,
`created`
FROM
`messages`
WHERE
`status` = 1
ORDER BY `created` DESC
LIMIT 1) T1
where `user_from` = $user and `user_to` != $user
If I understand your question correctly, do you want to do this:
SELECT
`text`,
`created`
FROM
`messages`
WHERE
`status` = 1
AND `user_from` = $user
AND `user_to` != $user
ORDER BY `created` DESC
LIMIT 1
Of course you need to replace $user with your string, or use a prepared statement to insert the variable into the query.
Select the latest message with status=1 and then use it as a nested query and check your conditions to output this one row result or not:
select `text`, `created`
from
(
SELECT
`text`,
`created`,
user_from,
user_to
FROM
`messages`
WHERE
`status` = 1
ORDER BY `created` DESC
LIMIT 1
) t
where (user_from = $user)
and (not (user_to = $user))

Exclude results of the first query on the second query in same table with MySQL

I want to do like this:
SELECT * FROM `langCategories` ORDER BY `name` ASC WHERE ORDER BY `amount` DESC LIMIT 8,0
but this is not posible.
I have 2 sections in my site:
1. Top 8 Populars Categories
(SELECT * FROM `langCategories` ORDER BY `langCategories`.`amount` DESC LIMIT 0,8)
2. The rest of Categories order by name (excluding the top 8)
(???)
I do it with php:
$var = db_multiselect("SELECT * FROM `langCategories` ORDER BY `amount` DESC LIMIT 0,8");
$var2 = db_multiselect("SELECT * FROM `langCategories` ORDER BY `name` ASC");
$i=-1;
while ($row = mysqli_fetch_array($var)) // Save the top8 categories
{
++$i;
$top8[$i] = $row["name"];
}
$i=0;
while ($row2 = mysqli_fetch_array($var2))
{
if (!(in_array($row2["name"], $top8))) // compare the rest of categories excluding top 8
{
...
}
}
But i want to do this with a MySQL Query, how can do it?
Subquery!
SELECT * FROM (
SELECT * FROM `langCategories`
ORDER BY `langCategories`.`amount` DESC
LIMIT 8,2000000000
) AS baseview
ORDER BY name ASC
Gives you the NOT top 8 categories in alphabetical order
this should work for MySQL (i hope):
SELECT *
FROM (
SELECT lc.*, #rownum:=#rownum+1 AS rownum
FROM `langCategories` lc, (SELECT #rownum:=0) r
ORDER BY `amount` DESC
)
ORDER BY CASE WHEN rownum <= 8 THEN rownum ELSE 999 END, name