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
Related
I am new to PHP and MySQL.
I wanted to know that if its possible to show all the rows from the table but first where id='some value'. ie.
SELECT * FROM users ORDER BY id='some value'
Thanks for answering.
You can make a set of queries, for example:
SELECT * FROM users WHERE id= 5 UNION SELECT * FROM users WHERE id != 5;
Or if you need something like this example:
SELECT * FROM users WHERE id >= 5;
Well, i found this simple script online somewhere for selecting the next row and selecting the previous row.
This works. (Next page)
$currentid = $_GET['id'];
$nextquery= mysqli_query($conDB,"SELECT * FROM vids WHERE ID > $currentid ORDER BY ID ASC LIMIT 1")or die (mysqli_error($conDB));
However, this dosent, it returns the current page id instead of the previous id.
What i want is that it gets the last available id that is on the database.
$prevquery= mysqli_query($conDB,"SELECT * FROM vids WHERE ID < $currentid ORDER BY ID desc LIMIT 1")or die (mysqli_error($conDB));
Please someone help me, i would greatly appreciate it! :D
Best regards Dániel
To find the largest value of the field "ID" from the "vids" table, you will want to use the following query:
SELECT ID FROM vids ORDER BY ID DESC LIMIT 1
To find the previous and next rows in the table, the queries you are using look correct, however there appears to be a typo in the first line calculating the $currentid:
$currrentid = $_GET=['id'];
This line should be:
$currrentid = $_GET['id'];
remove DESC for the previous query
"What i want is that it gets the last available id that is on the database."
`SELECT MAX(ID) FROM table_name;`
will give you the last record id in a table.
Next:
select * from table_name where ID = (select min(ID) from table_name where ID > $_GET['id'])
Previous:
select * from table_name where ID = (select max(ID) from table_name where ID < $_GET['id'])
Or you can optimise the query and have it all completed in one call:
select * from table_name where (
ID = IFNULL((select min(ID) from table_name where ID > $_GET['id']),0)
or ID = IFNULL((select max(ID) from table_name where ID < $_GET['id']),0)
)
Well I have this mysql table with numbers in one column and a confirmation boolean of 0 or 1 and I have about 1,000 rows so it's not something I can do manually but anyways...
I want to sort the row by highest value and grab the names of the first 5 people and put those 5 people in another table on a column and then set them to confirmed and continue until there's no one left in the table that isn't confirmed...
ex:
Name:Rank:Confirm
Bob:5000:0
James:34:0
Josh:59:1
Alex:48:0
Romney:500:0
Rolf:24:0
Hat:51:0
so when you run the code it will do the following:
Squad:Name1:Name2:Name3:Name4:Name5
1:Bob:Romney:Hat:Alex:James
(as you can see Josh was excluded and Rolf was too low)
And since Rolf is alone and there are no one else left, he wont be put into a team and will be left unconfirmed...
I'm not really pro at mysql so I was stumped on this and at most was capable of organizing the whole thing by rank and that's it ._.
edit:
The terrible attempt I had at this:
<?php
$parse = mysql_query("SELECT MAX(rank) AS rank FROM users AND confirm='0'");
mysql_query("Insert into squad (nameone)values($parse)");
mysql_query("Update squad set confirm = '1' where name = $parse");
?>
Assuming confirm will have only either 1 or 0.
CREATE TABLE table2 (id INT PRIMARY KEY AUTO_INCREMENT, name varchar(255));
CREATE PROCEDURE rank()
BEGIN
DECLARE count INT DEFAULT 1;
WHILE count > 0 DO
UPDATE table1 SET Confirm=2 WHERE Confirm=0 ORDER BY Rank DESC LIMIT 5;
INSERT INTO table2 (SELECT GROUP_CONCAT(Name) FROM table1 WHERE Confirm=2);
UPDATE table1 SET Confirm=1 WHERE Confirm=2;
SELECT count(*) FROM table1 WHERE Confirm=0;
END WHILE;
END;
Call the procedure rank() when ever you want
CALL rank();
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.
so I'm trying to create a ranking system for my website, however as a lot of the records have same number of points, they all have same rank, is there a way to avoid this?
currently have
$conn = $db->query("SELECT COUNT( * ) +1 AS 'position' FROM tv WHERE points > ( SELECT points FROM tv WHERE id ={$data['id']} )");
$d = $db->fetch_array($conn);
echo $d['position'];
And DB structure
`id` int(11) NOT NULL,
`name` varchar(150) NOT NULL,
`points` int(11) NOT NULL,
Edited below,
What I'm doing right now is getting records by lets say
SELECT * FROM tv WHERE type = 1
Now I run a while loop, and I need to make myself a function that will get the rank, but it would make sure that the ranks aren't duplicate
How would I go about making a ranking system that doesn't have same ranking for two records? lets say if the points count is the same, it would order them by ID and get their position? or something like that? Thank you!
If you are using MS SQL Server 2008R2, you can use the RANK function.
http://msdn.microsoft.com/en-us/library/ms176102.aspx
If you are using MySQL, you can look at one of the below options:
http://thinkdiff.net/mysql/how-to-get-rank-using-mysql-query/
http://www.fromdual.ch/ranking-mysql-results
select #rnk:=#rnk+1 as rnk,id,name,points
from table,(select #rnk:=0) as r order by points desc,id
You want to use ORDER BY. Applying on multiple columns is as simple as comma delimiting them: ORDER BY points, id DESC will sort by points and if the points are the same, it will sort by id.
Here's your SELECT query:
SELECT * FROM tv WHERE points > ( SELECT points FROM tv WHERE id ={$data['id']} ) ORDER BY points, id DESC
Documentation to support this: http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html
Many Database vendors have added special functions to their products to do this, but you can also do it with straight SQL:
Select *, 1 +
(Select Count(*) From myTable
Where ColName < t.ColName) Rank
From MyTable t
or to avoid giving records with the same value of colName the same rank, (This requires a key)
Select *, 1 +
(Select Count(Distinct KeyCol)
From myTable
Where ColName < t.ColName or
(ColName = t.ColName And KeyCol < t.KeyCol)) Rank
From MyTable t