How i can realize the next mysql query on zend? - mysql

SELECT * FROM (
SELECT `* FROM `messages`
ORDER BY `message_id` DESC
LIMIT 15
) AS temp
ORDER BY message_id ASC

$sub_select = $db->select();
$sub_select->from('messages');
$sub_select->order('message_id desc');
$sub_select->limit(15);
$select = $db->select();
$select->from(array('temp' => new Zend_Db_Expr('(' . $sub_select . ')')));
$select->order('message_id asc');
Doc : http://framework.zend.com/manual/1.12/en/zend.db.select.html

Related

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

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();

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

how to put if else condition in LIMIT mysql

here is my query
$where_or = "
AND
content_status = 'Active'
AND
content_post_status = 'published'
AND
deleted = 0
AND
content_type = '$type'
ORDER BY content_created_at DESC ";
$select = "content_id, content_title, content_short_description, content_url, content_created_at";
$query = $this->db->query("
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 54
$where_or
LIMIT 3
)
UNION ALL
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 55
$where_or
LIMIT 2
)
UNION ALL
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 56
$where_or
LIMIT 2
)
UNION ALL
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 57
$where_or
LIMIT 1
)
UNION ALL
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 58
$where_or
LIMIT 1
)
UNION ALL
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 60
$where_or
LIMIT 1
)
UNION ALL
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 61
$where_or
LIMIT 1
)
UNION ALL
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 118
$where_or
if content_type = 'article'
begin
LIMIT 10
end
)
");
You can see in last UNION i want to put limit on if condition ..but its giving me error...how can i do that...please help..
You cannot do that with MySQL, at least not the way you are trying to. If you think about it
this:
if content_type = 'article'
begin
LIMIT 10
end
doesn't make sense. LIMIT is applied to a set not a single record. If what you wrote were valid SQL and your data contained both content that was true for content_type = 'article' and some that was false. What would you expect to happen?
I'd suggest you to rank records and then add WHERE condtition to filter records by content_category_id and its rank. In this case you will avoid using multiple SELECT...FROM queries.

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

Order of "id" changed in subquery?

I have this query
SELECT bul.id
FROM bul
WHERE id IN (SELECT hotid AS parentid
FROM likehot
WHERE hotid IN (SELECT id
FROM bul
WHERE DATE >= '1315976410')
GROUP BY hotpid
ORDER BY COUNT( hotid ) DESC )
when I runt this inner query
SELECT id
FROM bul
WHERE DATE >= '1315976410')
GROUP BY hotpid
ORDER BY COUNT( hotid ) DESC
I get
parentid
3655
3656
3622
3644
and when I run whole query I get
parentid
3656
3655
3622
3644
I really don't understand why the order of the ids change?
SOLUTION :-
<?php
$query_hotpress_like = "SELECT hotid AS parentid FROM likehot WHERE hotid IN (SELECT id FROM bul WHERE DATE >= '" . (time() - (24 * 60 * 60)) . "') GROUP BY hotid ORDER BY COUNT( hotid ) DESC";
$exe_hotpress_like = execute_query($query_hotpress_like, true, "select");
$temp_like1 = array();
foreach ($exe_hotpress_like as $kk => $exe_like) {
$temp_like1[] = "'" . $exe_like['parentid'] . "'";
}
$temp_like = str_replace(",''", "", implode(',', $temp_like1));
$query_hotpress = "SELECT bul.id,photo_album_id,eventcmmnt_id,link_url,youtubeLink,link_image,id, mem_id, subj, body, bul.date,parentid, from_id, visible_to,image_link,post_via FROM bul WHERE id IN ($temp_like) ORDER BY FIELD(id,$temp_like ) LIMIT 5";
?>
execute_query() is the inbuilt function to get the result of query.
That happens because for IN operator order doesn't matter.
If you need to sort outer query - sort outer query.
Since you didn't specify an order for the "whole" query, the database is at liberty to return rows in any order it wants. The specific order you get is a result of how looking up rows is done when using the IN operator.
On your other query you are specifying an order yourself, so the database has to honor it.