I am trying to retrieve random rows from mysql using RAND() function. But in result rows are repeating more than once. Is there any way to return random rows in such a way that a row is returned only once?
My query is: SELECT h.recid recid, h.name name, h.subtitle subtitle, h.pricingfrom pricingfrom FROM holydays h ORDER BY RAND()
I found this solution but I didnt understand how it works as I don't have any limits...
Any answer will be appreciated.!
The ORDER BY clause of a query has nothing to with the result set, except maybe if LIMIT is being used, which it is not here. It sounds to me like your table has some duplicate records. If you want a random ordering with no duplicates, then select with DISTINCT:
SELECT DISTINCT
recid,
name,
subtitle,
pricingfrom
FROM holydays
ORDER BY RAND();
it's work by order by taking random columns
ex, there is id, name, emai then some time it order id,name,email or some time name,id,emai etc. etc. and it some time order in asc and some time in desc. That's logic.
Related
I have a table with 8,000 Records, I want to select randomly some records (for example 10) from this table. These records have to be different from previously selected records.
For example with this query I select some questions:
SELECT coloumn1,column2 FROM `myTable` WHERE `status`=1 AND `group`=6 ORDER BY RAND() LIMIT 0, :max
Now how can I select new records randomly and different from previously selected records?
Simply you could store the id's of previously selected records and then add to your next query like below, to avoid selecting them twice:
WHERE id NOT IN = (list of ids)
Based on you'r example:
SELECT coloumn1,column2 FROM `myTable` WHERE `status`=1 AND `group`=6 AND id NOT IN (_IDS_) ORDER BY RAND() LIMIT 0
try :
SELECT coloumn1, column2 FROM myTable WHERE status=1 AND group=6
AND coloumn1 NOT IN (id,id,id,id,ids concatenated in php)
ORDER BY RAND() LIMIT 0, :max
and also in select, better select a column that is unique identifier for the row, and use that column with the NOT IN ()
avoid RAND() as its extremely slow - better work around it with php
If the number of selected records is too big, put them in a temp table, and use
NOT IN (SELECT id FROM temp table...)
Im currently working on a project that requires MySql database and im having a hard time constructing the query that i want get.
i want to get the previous 10 rows from the specific WHERE condition on my mysql query.
for example
My where is date='December';
i want the last 10 months to as a result.
Feb,march,april,may,june,july,aug,sept,oct,nov like that.
Another example is.
if i have a 17 strings stored in my database. and in my where clause i specify that WHERE strings='eyt' limit 3
Test
one
twi
thre
for
payb
six
seven
eyt
nayn
ten
eleven
twelve
tertin
fortin
fiftin
sixtin
the result must be
payb
six
seven
Thanks in advance for your suggestions or answers
If you are using PDO this is the right syntax:
$objStmt = $objDatabase->prepare('SELECT * FROM calendar ORDER BY id DESC LIMIT 10');
You can change ASC to DESC in order to get either the first or the last 10.
Here's a solution:
select t.*
from mytable t
inner join (select id from mytable where strings = 'eyt' order by id limit 1) x
on t.id < x.id
order by t.id desc
limit 3
Demo: http://sqlfiddle.com/#!9/7ffc4/2
It outputs the rows in descending order, but you can either live with that, or else put that query in a subquery and reverse the order.
Re your comment:
x in the above query is called a "correlation name" so we can refer to columns of the subquery as if they were columns of a table. It's required when you use a subquery as a table.
I chose the letter x arbitrarily. You can use anything you like as a correlation name, following the same rules you would use for any identifier.
You can also optionally define a correlation name for any simple table in the query (like mytable t above), so you can refer to columns of that table using a convenient abbreviated name. For example in t.id < x.id
Some people use the term "table alias" but the technical term is "correlation name".
So far, I have taken 3 tables and joined them together. What I want to do is display the Last 100 entries (DESC) in ASC ORDER according to the timestamp in the column Posted.
This is as far as I could get: http://sqlfiddle.com/#!9/e2771/1
In addition, if there is a more efficient way to do this in PhP and not MYSQL, I'm all for that. I've tried looking, but haven't been able to find anything that works.
You just need one more level of sort:
select t.*
from (<your query here>) t
order by posted;
Whenever a query like
"select * from table where userid=xx" is done, the mysql fetches these values from
first row to last row of table.
But I want to select from last to first so that recently updated values are displayed first in the results.
I cannot do "select * from table where userid=xx order by time DESC" because there is no time column in table.
I just want recently updated items in the table displayed first.
$result= mysql_query("SELECT
(SELECT column FROM table WHERE [condition] ORDER BY column LIMIT 1) as 'first',
(SELECT column FROM table WHERE [condition] ORDER BY column DESC LIMIT 1) as 'last'");
$row=mysql_fetch_array($result);
echo $row['first'];
echo $row['last'];
If you have any auto-incrementing field you could sort by that desc.
You must provide a column to order by in order to guarantee results. So you will have to either change your table structure or make a decision on what to order by.
A hack would be to pull in the data in the order presented into an array, then start popping off the bottom of that array.
You either have to have a timestamp, or autoincrement id, or some other column that you want to sort by.
Just because you get rows in a certain order from a database when not using an ORDER BY clause, does not mean that they are guaranteed to be returned in that order. It also doesn't imply any order in the result set. You need a definitive field that you can use to ORDER BY, or you cannot do what you are wanting to do.
Some hack you can do for that if you don't have columns you can rely on:
SELECT * FROM (SELECT *,(#x:=#x+1) default_ordering FROM `table_name`, (SELECT #x:=0) t2) any_name ORDER BY default_ordering DESC
I have a table of customers with a 1 recorded against their customerid on different dates.
I would like to find the sum of the 1's recorded in descending order. I'm using MySQL and php
Thanks
My guess is that you want the sum of records marked with 1 per customer and sort that result in descending order? If so, the following should do the trick :
select cust.id, sum(cone.one) as number_ones
from customers as cust
inner join customer_ones as cone on cone.id=cust.id
group by cust.id
order by number_ones desc
This is assuming that 'one' is the column containing ones (and only contains 0 or 1 - otherwise you will have to add WHERE cone.one = 1), customers is your customer table and customer_ones is the table containing your customer data.
As i get you right, this is simple sql request what u need:
SELECT COUNT(id) as total from customers
Just make in php:
$sql="SELECT COUNT(id) from customers";
$query=mysql_query($sql) or die(mysql_error());
$res=mysql_fetch_assoc($query);
$summ=$res['total']; //<- Your summ (i.e. quantity of rows in table)
Btw, you can use mysql_num_rows instead.
Or explain please more accurately what output you need. To make sorting by date or any other dependency you will need to make other request using WHERE clause and date comparison.