This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySQL get row position in ORDER BY
I have a table of users and I want to order them by a column named crown. I then wanted to find out where they are in the list but not totally sure on how to do it. I have tried to Google it but not totally sure on what to type in. (Crown has INT input.)
So I have the first line:
mysql_query("SELECT * FROM users ORDER by crown DESC");
How would I then find out where a user is in the list while ordered by crown?
Thanks in advance.
I'd do something like this, assuming you can get the user's crown value.
SELECT `user_var_you_want`,
(SELECT COUNT(*) FROM `users` WHERE `crown` > $user_crown_value)+1 AS `position`
FROM `users`
WHERE `user_var_you_want` = $search_value
Or if you can't get their crown value:
SELECT `user_var_you_want`,
(SELECT COUNT(*) FROM `users` WHERE `crown` > (SELECT `crown` from `users` WHERE `user_var_you_want` = $search_value)+1) AS `position`
FROM `users`
WHERE `user_var_you_want` = $search_value
Related
This question already has answers here:
Using LIMIT within GROUP BY to get N results per group?
(14 answers)
Closed 2 years ago.
I´m building a php-blog system and want to display all posts but max five from each user on the start page.
I thinking of do this with a query in the database, but I´m lost on how to do that.
The count() function I guess will come in handy, but can somebody help me
This is my function today, and I just whant to improve it to get max five posts from each user
protected function getAllPostsDB() {
$sql = "SELECT recipes.Recipe_ID, recipes.Title, recipes.Short_description, recipes.Step_by_step,
recipes.create_date, recipes.last_mod_date, recipes.Portions, recipes.imgPath, users.Username
FROM recipes
JOIN users
ON recipes.User_ID = users.User_ID
ORDER BY recipes.create_date DESC";
$stmt = $this->connect()->query($sql);
/* fetch all is already set to associative array*/
$result = $stmt->fetchAll();
return $result;`
If you are running MySQL 8.0, just use window functions:
SELECT r.Recipe_ID, r.Title, r.Short_description, r.Step_by_step,
r.create_date, r.last_mod_date, r.Portions, r.imgPath, u.Username
FROM (
SELECT r.*, ROW_NUMBER() OVER(PARTITION BY User_ID ORDER BY create_date DESC) rn
FROM recipes r
) r
INNER JOIN users ON r.User_ID = u.User_ID
WHERE r.rn <= 5
ORDER BY r.create_date DESC
This gives the last five recipes per user, as designated by column create_date. You can change the ORDER BY clause of ROW_NUMBER() to some other column or set of columns if you want another sort rule.
This question already has answers here:
sorting by high-low price with mysql data
(2 answers)
Closed 7 years ago.
i have 2 tables
page and orders.
$ww = mysqli_query($database->connection,"SELECT * FROM `page`
WHERE `owner` = '$session->u_id'");
while($o = mysqli_fetch_array($ww))
{
$owner = $o['id'];
$result = mysqli_query($database->connection,"SELECT * FROM `orders`
WHERE `owner` = '$owner' ORDER BY id DESC");
while($row = mysqli_fetch_array($result))
{
echo "taxi =".$o['naam']."<br>";
echo $row['id']."<br>";
}
}
the output is
23
21
20
17
26
25
24
22
19
Question is How can i sort high to low like
26
25
24
23
22
21
....
You need to run just one query when selecting from orders, not one query per page (BTW, it helps if you tabulate properly, people can see this then). Even better is to just run one query using a join.
You'll need to post your table structure before we can write any query.
1) you can use inner join to write the query if id column of second table won't be null otherwise use left join
SELECT * FROM `orders` inner join page on orders.id = page.id where owner = $session->u_id;
2) you can use your first query as sub query and put it in second one like this
SELECT * FROM `orders` WHERE exists (SELECT page.id FROM `page` WHERE `owner` = '$session->u_id' and orders.id=page` WHERE `owner` = '$session->u_id' and orders.id= )ORDER BY id DESC.id ) ORDER BY orders.id DESC
Note : if any syntax error is found the forgive me :)-
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I would like to know which query is better. I want to select some specific records and all count of records in table(but filtered).
Which query is better?
SELECT name, surname,
(select COUNT(messages.id) from messages WHERE `messages`.`archived` = '0' AND `type` IN (0, 1) ) as count
FROM messages JOIN
users
ON users.id = messages.user_id
WHERE messages.archived = 0 AND type = 0 OR type = 1
ORDER BY created_on DESC
LIMIT 3
or
SELECT name, surname
FROM messages JOIN
users
ON users.id = messages.user_id
WHERE messages.archived = 0 AND type = 0 OR type = 1
ORDER BY created_on DESC
LIMIT 3
with
SELECT count(id) as count
FROM messages
WHERE `messages`.`archived` = '0' AND `type` IN (0, 1)
If I have big db I will check this out, but for now I have got a few records.
With single query the count is added to every record.
Thanks and sorry for my english!
After writing this, I realized that the two queries are not the same. So, you should use the version that corresponds the results you want to get. (Do you want the count filtered or not?)
I'm not sure if MySQL will optimize the subquery in the SELECT. However, you can just move it into the FROM clause, where it is only run once:
SELECT `name`, `surname`, m.cnt
FROM `messages` JOIN
`users`
ON `users`.`id` = `messages`.`user_id` CROSS JOIN
(select COUNT(messages.id) as cnt from messages) m
WHERE `messages`.`archived` = '0' AND `type` = 0 OR `type` = 1
ORDER BY `created_on` DESC;
LIMIT 3
I suspect your WHERE clause is incorrect. Do you really intend this?
WHERE `messages`.`archived` = '0' AND `type` IN (0, 1)
me again!
So I'm wondering what the sequel/php code is to display all the rows in a table except one row. So display row 1, 2, 3 and 4 and every column, but don't display anything to do with row 5. The reason for this is I want to display all users in a user table except the user that is looking at the list itself.
Checking which user is looking at the list is fine because I can use a variable I set aside called userid which can be used to check it against. I just don't know the SQL terminology I need to use to essentially "select * from table except this row".
Any help please?
What's wrong with:
SELECT * FROM `table` WHERE `id` <> 5
SELECT * FROM `users` WHERE `userID` != 5
Replacing 5 with the current user's ID should do the trick.
Why not just
SELECT * FROM table WHERE userid != 'currentUser'
$query = "SELECT * FROM users WHERE userId != $userId" ;
$userId can contain the user defined id
SELECT * FROM table WHERE NOT user_id = < id >
SELECT *
FROM (SELECT ROW_NUMBER()
OVER(ORDER BY id) RowNr, id FROM tbl) t
WHERE RowNr BETWEEN 10 AND 20
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
mySQL “Rank in Highscore”-Query
I was just wondering if there is any effective way to gather the current rank of some id based on points.
My only idea was to grab whole points and then check every record if its more or less. Something like this.
Select * from whatever table
$x=0
while($row = mysql_fetch_array($result) {
if ($user == $row['user']) {
echo "$user is rank $x out of " . count($result);
break;
}
else {
$x++
}
}
You can use a variable, as shown here:
SET #rank=0;
SELECT user.*, #rank:=#rank+1 AS 'rank'
FROM user
ORDER BY score DESC
Do you mean like the "MAX" in mysql?
"SELECT max(score) as record FROM users";
This will retrieve a column with the highest score from the users table.
this can be done in mysql:
SELECT ui2.*,
(
SELECT COUNT(*)
FROM users ui
WHERE (ui.points, ui.id) >= (ui2.points, ui2.id)
) AS rank
FROM users ui2
WHERE id = #id
Yes you can use max() along with the group by clause to group the results based on certain criteria.
"SELECT max(score) as record FROM users_points group by user_id, game_id";
drop table if exists users;
create table users
(
id int unsigned not null auto_increment primary key,
score int unsigned not null default 0
)
engine=innodb;
insert into users (score) values (10),(30),(50),(30),(20),(45),(20);
select
#r:=#r+1 as rank,
u.id,
u.score
from
users u
inner join (select #r:=0) as r
order by
u.score desc;