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
Related
how to use fetch 'food_store' order by randomly and price order by ASC ?
write now i am using this query
Select * from `food` where `veg_non` = 'Veg' and `food_price` <= ? ORDER BY RAND()
Try this:
Select *, RAND() as r from `food` where `veg_non` = 'Veg'
and `food_price` <= ? ORDER BY `food_price` ASC, r ASC
Nobody can answer this question?
$result=mysql_query("
SELECT COUNT(*) AS `total` FROM `mytable`
WHERE `myvariable`='1'
ORDER BY `id` DESC
LIMIT 15;"
);
$data=mysql_fetch_array($result);
$count = $data['total'];
echo $count;
This count ALL result from mytable, but how I can do to count last 15 results only? It seems LIMIT 15 not work in this case?
I think this is the query you want:
SELECT SUM(myvariable = '1') AS total
FROM (SELECT myvariable
FROM mytable
ORDER BY id DESC
LIMIT 15) AS subquery
This only looks at the most recent 15 rows, and counts the number of them that have myvariable = 1.
Since you want last 15 after descending order, Order by ascending and select first 15 and do descending order sort
select * from (SELECT * FROM mytable WHERE myvariable='1' ORDER BY id ASC LIMIT 15) ORDER BY id DESC
I have a simple MySQL query like this:
SELECT * ,
( MATCH (table.get) AGAINST('playstation ' IN BOOLEAN MODE) )
+ ( table.get LIKE '%playstation%') AS _score
FROM table
JOIN users on table.id_user = users.id
WHERE table.expire_datetime > 1375997618
HAVING _score > 0
ORDER BY RAND(table.id) ,_score DESC ;
If I run this query in MySQL, it returns usually more then 1 record, now I would like to LIMIT 1 and get one of them randomly, not always the same record.
Is it possible?
select * from <my_table>
ORDER BY RAND()
LIMIT 4
You would quit seeding the random number generator. My guess is that it is returning the first table id encountered, so the numbers are generated in the same sequence:
SELECT * ,
( MATCH (table.get) AGAINST('playstation ' IN BOOLEAN MODE) )
+ ( table.get LIKE '%playstation%') AS _score
FROM table
JOIN users on table.id_user = users.id
WHERE table.expire_datetime > 1375997618
HAVING _score > 0
ORDER BY RAND()
LIMIT 1;
As I understand problem in ,_score ?
Try this:
Select * FROM (
***your sql query***
) as t
ORDER BY RAND()
LIMIT 1
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
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.