I have a table where multiple records can be stored by the same user on a daily basis.
So right now I'm doing this:
SELECT * FROM `mytable` WHERE user_id = '$userid'
Now, what I need to do is to select the latest entry available by that user, something like:
The table has an ID field which is auto increment so, I was thinking something like:
SELECT * FROM `stats` WHERE user_id = '$userid' WHERE ID <- Is the latest..
How can I do that and select Where ID is the latest ?
How about
SELECT *
FROM `stats`
WHERE user_id = '$userid'
order by ID desc limit 1
You can do a subquery.
SELECT * FROM `stats` WHERE user_id = '$userid' WHERE ID = (SELECT MAX(ID) FROM your_table)
See Abhik Chakraborty's answer if the higher ID you search is from the "stats" table. It's answer is a lot better :)
try this query.
SELECT * FROM mytable WHERE postID=(SELECT MAX(userID) FROM mytable)
Related
I have a database with direct messages from one user to another.
Table name: dm
id int(11) Primary Key
to: varchar
from: varchar
message: varchar
I have a SQL Statement that selects the distinct values from the to and from column as if they were one column. ($username is a session variable)
"SELECT DISTINCT from
FROM dm
WHERE to = '$username'
UNION
SELECT DISTINCT to
FROM dm
WHERE from = '$username'
";
I am checking to see if this user has received or sent any messages basically. I want to ORDER BY id of dm. How can I change my code so I get the same results and it orders by id DESC? If I simply enter ORDER BY like so, I get an error because I didn't select it... Thank you so much for the help
"SELECT DISTINCT from
FROM dm
WHERE to = '$username'
UNION
SELECT DISTINCT to
FROM dm
WHERE from = '$username'
ORDER BY id DESC
";
If I got exactly what you wanted to do you should keep things separate:
1st step: your original code, plus the user ID added to the dataset
CREATE TABLE new_table_name AS
SELECT DISTINCT ID, from
FROM dm
WHERE to = '$username'
UNION
SELECT DISTINCT ID, to
FROM dm
WHERE from = '$username';
2nd step: order by ID
CREATE TABLE ordered_table AS
SELECT *
FROM new_table_name
ORDER BY ID DESC;
I have a table like below
ID Status
1 InActive
2 Active
3 Active
4 InActive
5 Active
I want to select ID 2 then get the status of ID 2 then based on that status I want to select all the remaining ID with the same Status.
I was not able to create a simple demo because I cant think of a way how to select.
Expected:
Get ID 2,3,5 because they all have active status
Currently working on the problem at hand
Base select:
Select * from table where id = 2
I want to know what to add to this query to be able to select the other 2 row that share same status with the id 2
How about using a subquery to find the ID 2 status:
SELECT *
FROM yourTable
WHERE Status = (SELECT Status FROM yourTable WHERE ID = 2)
You should use JOIN for your result.
SELECT *
FROM `tablename` AS t1
JOIN `tablename` AS t2 ON t1.Status = t2.Status where t1.ID = 2
Use the following query:
SELECT `ID`
FROM `table`
WHERE `Status` = (SELECT `Status` FROM `table` WHERE `ID` = 2)
Trying to create a query that gets the most recent record related to the user. Here's my attempt
"SELECT *
FROM (
SELECT *
FROM mytable
ORDER BY id
DESC LIMIT 1)
WHERE userid = $userID";
You could also:
Select * from table
Where id =
(Select Max(Id) from table
where userid = $userID)
Simply skip the sub-query:
SELECT *
FROM mytable
WHERE userid = $userID
ORDER BY id DESC
LIMIT 1
Use Top(1)
Select TOP(1) * FROM table ORDER BY id DESC
Try:
SELECT *
FROM mytable
WHERE userid = $userID
ORDER BY id DESC
LIMIT 1
I want to create a sub-account system (PHP & MYSQL).
I have a user table (users) and a sub users table (sub_users).
How can check if the user is available in the user table, or in the sub users table?
My code:
SELECT DISTINCT *
FROM users
WHERE userid = "steven"
OR WHERE EXISTS (SELECT *
FROM sub_users
WHERE sub_users.userid = "steven");
ERROR: Check your syntax near "steven"
Also tried:
SELECT *
FROM users
LEFT JOIN sub_users
ON sub_users.user_userid = users.userid
WHERE users.userid = 'steven'
OR sub_users.userid = 'steven'
Same error.
In the first place, you only want one where clause. I would also use single quote instead of double quotes:
SELECT DISTINCT *
FROM users
WHERE userid = 'steven' or
EXISTS (SELECT *
FROM sub_users
WHERE sub_users.userid = 'steven'
);
I doubt you need the distinct keyword, if you are fetching all the columns from users.
Your second query looks ok. Are you sure you are not running the first query twice?
EDIT:
I'm trying to figure out what you want to return. The following returns 1 if 'steven' appears in either table and 0 otherwise:
select (case when exists (select 1 from users where users.userid = 'steven') and
exists (select 1 from sub_users where sub_users.userid = 'steven')
then 1
else 0
end);
This method saves on the overhead of a join and will readily take advantage of indexes on users(userid) and sub_users(userid).
You could also use a union:
(SELECT userid FROM users WHERE userid = 'steven')
UNION
(SELECT userid FROM sub_users WHERE userid = 'steven');
select * from
(select * from users) x,
(select * from sub_users) y
where x.user_id = 'steven' or y.user_id = 'steven'
Good luck !!!
How we can do something as follows?
$results = $DB->query("SELECT * FROM users WHERE id!='{$id}' ");
Even I don't know to code,
$a = $DB->query("SELECT * FROM friends WHERE(someone is not friends with you)' ");
Columns in friends: user_id, friends_id
$DB->query("SELECT x.* FROM users x WHERE x.id NOT IN (myid, friendids)");
Use NOT IN
ID NOT IN (IDs OF FRIEND)
Example
$DB->query("SELECT * FROM user AS User WHERE User.id NOT IN (select * from friends where user_id = User.id)");
You need to get the persons whom are not your friends. right?
So you have your user_id as $my_id. Then you can select the friend_ID's from friends for whom the user_id field does not contain your user_id value($my_id). Then from those friend_ID's retrieved, you can select the users from Users table.
Use this:
$DB->query(SELECT * FROM users WHERE id IN(select friend_id FROM
friends WHERE user_id != {$my_id}))
Will work. :)
Not knowing the exact structure of your db makes your question a bit trick however going by the idea that someone is said to be your friend when he is your friend or you are his friend, you have to select those who you didn't add as friend and they as well didn't add you.
A query like this should help you.
"SELECT * FROM users WHERE users.id NOT IN (select * from friends where user_id != '{$your_id}' AND friend_id != '{$your_id}')"