I'd like to use RAND() in a query to be able to do the following:
ODER BY id DESC and allow RAND() to choose between last 3 inserted rows in the table. On the front-end when page is refreshed function rand will choose between 5 - 8 (on the table example) and show any data between those numbers.
Query Example
function rand()
{
$sth = $this->db->prepare("SELECT rows FROM table ORDER BY id LIMIT 1");
$sth->execute();
}
Table Example
+--------------+
| id | name |
+--------------+
| 1 | Jon |
| 2 | Sarah |
| 3 | Stevie |
| 4 | Stew |
| 5 | Dave |
| 6 | Kar |
| 7 | Stevo |
| 8 | Blake |
+----+---------+
EDIT
+----+
| id |
+----+
| |
| |
| |
| |
| |
| |
| |
| || |
If I understand your question correctly, I think you need this:
SELECT id, name
FROM
(SELECT id, name FROM table ORDER BY id DESC LIMIT 3) s
ORDER BY rand()
LIMIT 1
Have you tried this:
SELECT name
FROM users
ORDER BY RAND()
LIMIT 3
http://davidwalsh.name/mysql-random
Related
I wrote this sql query and it works but it is taking long time to be executed
SELECT trans_files.id, trans_files.game_name, trans_files.file_name,
COUNT(en_txt.id) as txt_num, COUNT(ar_txt.id) as n_txt_num
FROM trans_files
LEFT JOIN en_txt ON en_txt.file_id = trans_files.id
LEFT JOIN ar_txt ON ar_txt.en_text_id = en_txt.id
&& ar_txt.date = (SELECT MAX(ar_txt.date)
FROM ar_txt
WHERE en_text_id = en_txt.id)
GROUP BY trans_files.id
ORDER BY trans_files.id DESC, trans_files.game_name ASC, trans_files.file_name ASC
..
mysql> select * from ar_txt limit 3;
+----+------------+---------+----------------+---------------------+
| id | en_text_id | user_id | text | date |
+----+------------+---------+----------------+---------------------+
| 1 | 16 | 3 | Ϻ┘äÏ▓Ï╣┘è┘à | 2017-01-24 19:10:19 |
| 2 | 18 | 3 | Ϻ┘äϡϻϺϻ | 2017-01-24 19:13:36 |
| 3 | 3 | 3 | Ϻ┘äÏÀÏ¿┘èÏ¿┘è | 2017-01-24 19:15:48 |
+----+------------+---------+----------------+---------------------+
mysql> select * from en_txt limit 3;
+----+---------+------------------+
| id | file_id | text |
+----+---------+------------------+
| 1 | 2 | Apothecary Cheng |
| 2 | 2 | Blacksmith Ho Li |
| 3 | 2 | Apothecary Sung |
+----+---------+------------------+
mysql> select * from trans_files limit 3;
+----+-----------+-----------+
| id | game_name | file_name |
+----+-----------+-----------+
| 1 | drb | skills |
| 2 | drb | npcs |
| 3 | drb | test |
+----+-----------+-----------+
in this case i am using PDO and query to execute it
PHP code
so why does it take a long time?
I am doing a script want to calculate how many row record before an user record when t1.status is 1.
My table is t1, and the data as below:
+------+---------+------------+----------+----------+
| ID | name | desc | status | time |
+------+---------+------------+----------+----------+
| 1 | ABB | | 1 | 0325 |
| 2 | CCD | | 1 | 0236 |
| 3 | EEF | | 1 | 0325 |
| 4 | GGG | | 1 | 0000 |
| 5 | HIJ | | 2 | 1234 |
| 6 | KKK | | 1 | 5151 |
+---------------------------------------------------+
I was thinking about the query is something like (query row where status = 1 AND stop when reach $userid)
I would like to output to show user (Let's say username is GGG) as:
$userid = 'GGG';
then my output will be
<table><tr><td>Queue: GGG You came in 4 place, in front of you still got 3 person in queue, please be patient</td></tr></table>
How to I do the right query to get the number 4 and 3 ?
Thank you.
You can try something like this hope it helps :-
SELECT count(*) as COUNT FROM t1 WHERE id < (SELECT id FROM t1 WHERE userid = $userid)
I have 3 table now:
First is : member_username
+-------------+------------------+
| uid | username |
+-------------+------------------+
| 1 | theone |
| 2 | ohno |
| 3 | prayforpr |
+-------------+------------------+
Second is : member_data
+-------------+-------------------+-----------------+
| uid | talk | etc |
+-------------+-------------------+-----------------+
| 1 | talk1 | |
| 2 | talkeee | |
| 3 | iojdfnl | |
+---------------------------------------------------+
Third is : member_level
+-------------+-------------------+-----------------+
| uid | level | fid |
+-------------+-------------------+-----------------+
| 1 | 2 | 1 |
| 1 | 10 | 2 |
| 2 | 1 | 1 |
| 2 | 99 | 2 |
| 1 | 40 | 3 |
| 3 | 50 | 1 |
| 1 | 44 | 4 |
+---------------------------------------------------+
I would like to query data and display the only one uid when member_level is higher in when SUM member_level.level Where fid in 1,2,3.
my query now is like below, but this query is sum all the level including fid 4 also, how to specify only sum in fid 1,2,3? and how do I assign the SUM of member_level.level Where fid in 1,2,3 to $levelKingTotalLevel?
$levelKing = DB::query("SELECT t1.uid,t1.username,t2.talk FROM ".DB::table('member_level')." t3 JOIN ".DB::table('member_username')." t1 ON(t3.uid = t1.uid) JOIN ".DB::table('member_data')." t2 ON (t1.uid = t2.uid) GROUP BY t3.uid ORDER BY SUM(t3.level) DESC LIMIT 1");
while($rowlevelKing = DB::fetch($levelKing)) {
$levelKingTotalLevel = $rowlevelKing['???'];
$levelKingN = $rowlevelKing['username'];
$levelKingUID = $rowlevelKing['uid'];
$levelKingT = $rowlevelKing['talk'];
};
echo "The ".$levelKingN." total level is ".$levelKingTotalLevel." and he talk about ".$levelKingT;
Thank you.
To filter records having fid values as 1, 2 or 3, use IN statement in WHERE clause. Alias totalLevel in select statement will give you total level for a user.
SELECT t1.uid, t1.username, t2.talk, SUM(t3.level) AS totalLevel
FROM member_level t3
JOIN member_username t1
ON (t3.uid = t1.uid)
JOIN member_data t2
ON (t1.uid = t2.uid)
WHERE t3.fid IN (1,2,3)
GROUP BY t3.uid
ORDER BY totalLevel DESC
LIMIT 1
I have the following table and I want to select all (via php query) but only the rows that matches course1, I tried the following but it doesn't work.
$comments = array();
$result = mysql_query("select * from comments where course1='$post_id' order by id ASC");
what's the right query?
+----+-----------+----------+-----+-------------------------+---------------+-------------
| id | post_id | name | url | email | body | dt |
+----+-----------+----------+-----+-------------------------+---------------+-------------
| 1 | course2 | john | | john#john.com | comments by john | 2012-11-16 |
| 2 | course1 | wiki | | wiki#wiki.com | comments by wiki | 2012-11-16 |
| 3 | course2 | daniel | | daniel#gmail.com | comments by daniel | 2012-11-16 |
| 4 | course2 | ram | | ram#ram.com | comments by ram | 2012-11-16 |
| 5 | course1 | velu | | velu#velu.com | comments by velu | 2012-11-16 |
+----+-----------+----------+-----+-------------------------+---------------+-------------
Try this: SELECT * FROM comments WHERE post_id='course1' order by id ASC
I think you'd muddled your column names and search values in your sql
try this:
$result = mysql_query("select * from comments where post_id='$post_id' order by id ASC");
You need to reference the field name you're searching on:
$result = mysql_query("select * from comments where post_id='$post_id' order by id ASC");
Where your $post_id string variable contains value "course1".
I have a challenge I can't seem to handle.
+------+--------+-----------+-------+
| id | user | genres | books |
+------+--------+-----------+-------+
| 1 | John | crimes | 2 |
| 2 | John | scienc | 1 |
| 3 | John | nature | 4 |
| 4 | Pete | nature | 3 |
| 5 | Pete | crime | 2 |
| 6 | Mary | nature | 20 |
+------+--------+-----------+-------+
I would like to have a SQL query that gets the total amount of books the users own, no matter the genre and would like to ORDER them by who has the most.
In this example, you see that Mary has 20 books, Pete 5 and John has 7 so my desired result would be an array like:
result[0][user] = "Mary";
result[0][total] = 20;
result[1][user] = "John";
result[1][total] = 7;
result[2][user] = "Pete";
result[2][total] = 5;
How can I get this into one SQL? Should I use CONCAT or TOP or something? I use MySQL & PHP.
You need GROUP BY with SUM
SELECT `user`, SUM(books) AS total_books
FROM `table`
GROUP BY `user`
ORDER BY total_books DESC
If you only want the first 10 then you can use
SELECT `user`, SUM(books) AS total_books
FROM `table`
GROUP BY `user`
ORDER BY total_books DESC LIMIT 10`
By the way, you might want to rethink your schema slightly. Duplicating info is against the principles of normalisation. You might want to add a new owners table:
+-----------+-------------+
| owner_id | owner_name |
+-----------+-------------+
| 1 | John |
| 2 | Pete |
| 3 | Mary |
+-----------+-------------+
and then reference this by owner_id in your books table.
select user, sum(books) as total
from your_table
group by user
order by sum(books)
limit 10
SELECT sum(books) as book_count, user FROM `books` GROUP BY (user) order by book_count DESC