I have the mysql query:
SELECT * FROM bigtable WHERE column1='1' ORDER BY column2 DESC LIMIT 10
And then I put everything in an array and use php to choose a random row from this array of 10 items.
Is there a way to do this with a single mysql query instead of mysql+php part?
After take top 10, then take 1 with random:
SELECT * from (
SELECT * FROM bigtable
WHERE column1='1'
ORDER BY column2 DESC LIMIT 10
) T ORDER BY RAND()
LIMIT 1
Related
i have a table in mysql with columns: id , view , agree.
i have upload my table's image below:
i want to select 8 rows that greater than others in view column. my condition is agree = 1.
can you tell me how can i do it by mysql query or php.
Select * from table_name WHERE agree = 1 ORDER BY view desc LIMIT 8
Try this:
SELECT * from table
WHERE agree = 1
ORDER BY view DESC
LIMIT 8
use limit and order by
Select * from mytable
where aggree=1
ORDER BY view DESC
LIMIT 8
You can do this:
SELECT *
FROM yourTableName
WHERE agree = 1
ORDER BY view DESC
LIMIT 8
You have to use ORDER BY clause.
SELECT * FROM <TABLE_NAME> WHERE AGREE = 1 ORDER BY VIEW DESC LIMIT 8
Use ORDER BY for DESC or ASC sorting.
and For selection of 8 rows use Limit Data Selections From a MySQL Database
Select * from table_name WHERE agree = 1 ORDER BY view desc LIMIT 8
If you want to take the top 8 'view' values from the table the MySql query for that is:
SELECT * FROM tablename WHERE agree=1 ORDER BY view DESC LIMIT 8;
Note: Replace tablename with the actual name of your table
I must execute the query below in PHP with SQLServer, however I can not use the missing LIMIT clause in Microsoft queries.
$SqlTabelaAtual="SELECT *
FROM BusinessCadTabPreco
RIGHT JOIN BusinessCadTabPrecoItem ON BusinessCadTabPreco.CdTabela = BusinessCadTabPrecoItem.CdTabela
WHERE BusinessCadTabPreco.CdEmpresa =01
AND CdProduto =".$row['CdProduto']."
ORDER BY BusinessCadTabPreco.DtSincronizar DESC LIMIT 1
Use this code, in SQLServer the keyword Limit is TOP
$SqlTabelaAtual="SELECT TOP 1 *
FROM BusinessCadTabPreco
RIGHT JOIN BusinessCadTabPrecoItem ON BusinessCadTabPreco.CdTabela = BusinessCadTabPrecoItem.CdTabela
WHERE BusinessCadTabPreco.CdEmpresa =01
AND CdProduto =".$row['CdProduto']."
ORDER BY BusinessCadTabPreco.DtSincronizar DESC"
You can limit the records returned by adding the TOP expression to your SELECT clause.
Usage
/* Returns the first 10 records.
* Combine with an ORDER BY if you want control over the records returned.
*/
SELECT TOP 10
Id
FROM
TableName
;
Or
-- Percent return.
SELECT TOP 10 PERCENT
Id
FROM
TableName
;
i have a MySql table that consists of 2 basic things:
The id and a value.
To show that on my page, i need to select, for example, the last 100 rows on reversed order.
So imagine that someone is putting data on it:
Id, value
1, 10
2, 9
3, 21
4, 15
i need, to select the last "3" rows (LIMIT + ORDER Clause), but not like this: 4,3,2 but like this: 2,3,4.
I know how to do that on code, but maybe there is a simple solution for that on Mysql and i don`t know.
Thanks
My SQL Query is like this right now:
SELECT `Data`.`id`, `Data`.`log_id`, `Data`.`value`, `Data`.`created` FROM `control_panel`.`datas` AS `Data` WHERE `Data`.`id` > 1000 AND `Data`.`log_id` = (2) ORDER BY `Data`.`id` DESC LIMIT 100
You need to wrap the first ORDER BY in a subselect which will return a limited selection ordered in descending order, then you can order that result in the outer query in ascending order:
SELECT
a.*
FROM
(
SELECT id, value
FROM tbl
ORDER BY id DESC
LIMIT 3
) a
ORDER BY
a.id
One way to do this would be with a sub-select.
SELECT *
FROM (SELECT * FROM table_name ORDER BY id DESC LIMIT 3) tmp
ORDER BY id ASC
simply
SELECT t.*
(SELECT * FROM table_name
ORDER BY column_name DESC
LIMIT 0,3) t
ORDER BY t.column_name ASC
use DESC to descending order, ASC to increasing order
Simply I have the following
table with records , i want to return randomly 5 records from the last 20 record (order by id desc)
so how we can do it fast
thanks for help.
select * from
(
select * from your_table
order by id desc limit 20
) as lastest_results
order by rand()
limit 5;
Use an inner query to return the last 20, and an outer query to select 5 of them randomly. This may be slow though.
SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 20) t ORDER BY RAND() LIMIT 5;
This is a slow method but it gets the JOB done:
ORDER BY RAND()
LIMIT 5;
If you are using a large table this can become very slow,
There are multiple alternative that you can read about here
Alternative to order by rand
You need something like
SELECT * FROM table ORDER BY RAND() LIMIT 5;
If you have a time parameter to sort them by for example the last 20 records then use this.
SELECT * FROM table ORDER BY insert_time DESC, RAND() LIMIT 5;
OR
SELECT * FROM table ORDER BY id DESC, RAND() LIMIT 5;
I need help on how to randomize the last 10 rows of MySql records.
$mysqld = mysql_query(select * from table where amount > amount2 and code = '$code' order by time DESC limit 1);
From the statement above I need to randomize the last 10 rows ordered by time but limited only 1 to display.
EDIT: In other words, I need to have the table ordered by time and then I need to focus on the last 10 rows. From these last 10 rows, I need to pick one and it must be random, which one I get.
Is this possible?
Thanks
Assuming that time is the time when record was inserted, this will get you the latest 10 rows from the table:
SELECT * FROM `table` WHERE `amount` > `amount2` AND `code` = '$code'
ORDER BY `time` DESC LIMIT 10
Now, you can use the result as a temporary table, sort it randomly (as it's only 10 rows) and return one row:
SELECT * FROM (
SELECT * FROM `table` WHERE `amount` > `amount2` AND `code` = '$code'
ORDER BY `time` DESC LIMIT 10
) AS temptable
ORDER BY RAND()
LIMIT 1
Try....
SELECT * FROM (SELECT * FROM yerTable ORDER BY id DESC LIMIT 10) AS tmp ORDER BY RAND() LIMIT 1
Obviously replace id with any other distinct column if preferred.