I have a sql query for getting first 40 list of users.I want to retrieve one user always in that list.Is their any method in query specifying the user id with the limit
The best way I can think of is:
SELECT * FROM tbl WHERE userid='your-user-id' UNION SELECT * FROM tbl WHERE userid!='your-user-id' LIMIT 39
Basically, you select your user, and then you select 39 others. You use UNION to conjoin the two SELECT results.
SELECT
*
FROM
`users`
WHERE
`user_id` != 12345
LIMIT 39
UNION SELECT
*
FROM
`users`
WHERE
`user_id` = 12345
ORDER BY `user_id`
;
This will give you first 39 users + user with user_id=12345.
Select * from table order by userd_id limit 40
I guess that would be
(SELECT * from users limit 39)
UNION ALL
(SELECT * from users where userid='your-user-id')
As most of the options are already provided, check if this can help you
select name from user_details where id = user_id || id != user_id order by field (id,user_id) desc limit 40;
This will give you combine results & if you want your specified user id will always come on top.
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 want to select a random userid from this query where level = 1 (Normal user) and alive = Yes (User is alive and can play)
$getaid = $sql->query("SELECT userid FROM `users` WHERE `level`='1' AND `alive`='yes'");
I know I can use
$totalusers = mysql_num_rows($getaid);
$randomuserid = rand(1,$totalusers);
to select a random userid but in that code there's a chance that it select a user that is dead (alive = No) or a staff member (level >= 1). is there a better way I can select a random userid without a chance to grab staff members/dead members?
You can do
"SELECT userid FROM `users` WHERE `level`='1' AND `alive`='yes' ORDER BY RAND() LIMIT 1"
Instead of selecting all the records and trying to choose a random one in PHP's side, let the database do the heavy lifting for you:
SELECT userid
FROM `users`
WHERE `level` = '1' AND `alive` = 'yes'
ORDER BY RAND()
LIMIT 1
Your method is about the worst of all possible worlds -- you are bringing all the data back from the database to an array in PHP and then choosing a random value there. But, it should work, because you are only bringing back the appropriate users.
One way of doing this in the database that is simple enough is:
SELECT userid
FROM `users`
WHERE `level`='1' AND `alive` = 'yes'
ORDER BY rand()
LIMIT 1;
However, if you have more than a few hundred rows (or a few thousand), this starts to get too expensive. There are definitely other methods, but bringing all the data back is not a good idea.
One simple fix is to get approximately 100 rows and then randomize them:
SELECT userid
FROM `users` CROSS JOIN
(SELECT COUNT(*) as cnt FROM users `level` = '1' AND `alive` = 'yes') x
WHERE `level` = '1' AND `alive` = 'yes' AND
rand() < 100 * 1 / x
ORDER BY rand()
LIMIT 1;
For performance, you want an index on `users(level, alive).
You can use ORDER BY RAND() LIMIT 1:
$getaid = $sql->query("SELECT userid FROM `users` WHERE `level`='1' AND `alive`='yes' ORDER BY RAND() LIMIT 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
Im wondering if someone could help out.
I need to write a query that gets the last 3 'created' records, but their UID has to be unique so for example, my mysql fields look like so
uid created
19 2012-02-01 01:08:43
18 2012-02-31 17:07:21
19 2012-02-31 16:07:20
20 2012-02-31 13:03:00
Ok, so i want to get the last 3 uid's created ... but they have to be unique uid's so the same uid cant appear twice.
Cheers
this should do this
SELECT * FROM ( SELECT * FROM tablename ORDER BY uid, created DESC ) ordered GROUP BY uid
You can select last 3 IDs with this query:
SELECT * FROM ( SELECT * FROM table ORDER BY uid, created DESC ) AS selected GROUP BY uid LIMIT 3
Logic is: order table by uid field in descending and limit to 3 fields.
Use a Distinct and Group by to get what you're after:
SELECT DISTINCT * FROM table GROUP BY uid;
That will give you all unique UID's.
Then add your ORDER BY in there to make sure you grab the last records.
SELECT *
FROM table
ORDER BY created DESC
GROUP BY uid LIMIT 0,3
SELECT uid
, MAX(created) AS max_created
FROM tableX
GROUP BY uid
ORDER BY max_created DESC
LIMIT 3
I have a database with users and points (in fact it's a percentage, but that doesn't matter). The user(s) with the highest number of points is on the first rank, the second on the second rank ...
I could get the rank of a $searchedUserID if I did somethink like this:
SELECT `user_id`, `points` FROM `usertable` ORDER BY `points` DESC
/** This function returns the rank of a user. The rank nr. 1 is the best.
* It is possible that some users share a rank.
*
* #param int $searchedUserID the ID of the user whose rank you would like to
* know
*
* #return int rank
*/
function getUserRank($searchedUserID)
{
$userArray = getAllUsersOrderedByPoints();
$rank = 0;
$lastPoints = -1; // Never happens
foreach ( $userArray as $user) {
if ($user['point'] != $lastPoints) $rank++;
if ($user['user_id'] == $searchedUserID) break;
}
return $rank;
}
Isn't there a more direct way to get this with (My)SQL?
If not: Can the PHP-part be improved?
(edit: I could store the rank calculated by PHP directly in the database ... but this would mean I had to make quite a lot of UPDATEs.)
edit2: Perhaps GROUP BY could be used? Something like:
SELECT `user_id`, `points` FROM `usertable` GROUP BY `points` ORDER BY `points` DESC
The problem with this query is the possibility, that I don't get the searched user_id. It would be necessary to send a second query:
SELECT `user_id` FROM `usertable` WHERE `points` = $pointsOfTheUser
You ask:
Isn't there a more direct way to get this with (My)SQL?
Yes. In SQL:2003, you could use the DENSE_RANK() window function. In MySQL, you can simulate this, as the (dense) rank of a record by some score is simply the count + 1 of distinct better scores:
SELECT u.user_id,
u.points,
1 + COUNT(DISTINCT others.points) AS `dense_rank`
FROM users u
LEFT JOIN users others
ON u.points < others.points -- Which other users have more points?
WHERE user_id = ?
GROUP BY 1, 2;
perhaps an inner join with group by and sort would do the trick?
SELECT * FROM
INNER JOIN
(
SELECT user_id AS uid, max(points) AS score
FROM usertable GROUP BY user_id
)
AS ds ON usertable.user_id = ds.uid AND usertable.points = ds.score
ORDER BY score DESC
Just thinking on paper (pixels) .. wouldn't that give you the list in order from highest points to lowest with a unique record per user ... or are you looking to have these sorted where you can clarify a tie as a single "place" in the rankings?