$sql = "SELECT user2 FROM subscriptions WHERE user1 = '$username'";
This code works perfectly, but what I would like to do is ORDER it BY a column from my users table, the user_id column. Now I can't do this…
$sql = "SELECT user2 FROM subscriptions WHERE user1 = '$username' ORDER BY user_id";
Because there is no column called user_id in my subscriptions table, just in my users table.
I can't understand why you have two user columns, maybe it's a typo. But you need to JOIN the tables. In this case I've used a LEFT OUTER JOIN which will only return records from subscriptions and what matches the JOIN condition from users. i.e. user2 = user_id.
SELECT subscriptions.user2
FROM subscriptions
LEFT JOIN users ON subscriptions.user2 = users.username
WHERE subscriptions.user1 = '$username'
ORDER BY users.user_id
You can select the user_id in your ORDER BY clause:
SELECT user2
FROM subscriptions
WHERE user1 = '$username'
ORDER BY (select user_id from users where users.username = subscriptions.user2);
I surmise that users.username is unique, so you have two natural unique keys in your users table (user_id and username). You've decided to use username for references in other tables. That is okay.
If, however, users.username is not unique, then your databse design is broken and you should use the user_id as reference in other tables instead of username, of course.
Use regular join (not LEFT JOIN) if you have one-to-one data relation.
SELECT a.user2 FROM subscriptions a, user b
WHERE a.user2 = b.username
AND a.usera.user1 = '$username' ORDER BY b.user_id
This is hard to give working solution without viewing table's scheme. But from the query it's looks like you have poor dab design if you use '$username' to search in subscriptions instead of users table.
With an good database design your query should be like this:
SELECT a.fiel1, a.field2, a.user_id, b.field1, b.field2, b.user_id
FROM subscriptions a, user b
WHERE a.user_id = b.user_id
AND b.username = '$username' ORDER BY b.user_id
Joins by user_id
Storing username once in users table
Use user_id in any other tables
You should use JOIN syntax and after you can order by user_id from the users table.
u should use order by user_id desc
Related
I have been looking for a solution for this for over an hour now and decided to resort to asking here.
I am creating a "Twitter-like", following system for users of my Website and I wanted to be able to display each and every one of the users that a specific user follows or is followed by, I also want to then order this by the timestamp on the follow table, descending so that the latest follower is at the top.
The solutions I have come across seem to use inner joins etc. which is all well and good, but I was wondering whether there is a logical solution for my current query to do this.
Table structures:
users:
id | username
follows:
id | follower_id | following_id | timestamp
My current query:
SELECT * FROM users WHERE id IN (SELECT follower_id FROM follows WHERE following_id = $user_id) ORDER BY id ASC
Of course this will simply order by the user ID, how would I (using the current query structure), be able to add the order to list by the follows timestamp?
MySQL INNER JOIN
"SELECT users.* FROM users
INNER JOIN follows ON follows.follower_id = users.id
WHERE follows.following_id = $user_id
ORDER BY follows.timestamp DESC";
You can sort using multiple columns like this:
ORDER BY [column1] [ASC|DESC], [columm2] [ASC|DESC], ...
Therefore, edit your query's order by clause to include the second column and sort it descending.
You must use a join to add the column; here's the basic syntax of a join:
SELECT [table_name].[column_name], ...
FROM [table1]
JOIN [table2] ON [join condition]
...
Your code should look somewhat like this:
SELECT users.*
FROM users
JOIN follows ON users.id = follows.following_id
WHERE follows.following_id = $user_id
ORDER BY users.id ASC, follows.timestamp DESC
As far as I know, there is no way to do this without joining the tables; perhaps its possible to sort the returned list, but no guarantees):
SELECT * FROM users
WHERE id IN (SELECT follower_id FROM follows
WHERE following_id = $user_id
ORDER BY timestamp DESC)
ORDER BY id ASC;
The above may or may not work (I didn't test it); if it does not, you must use a join query.
I am trying to understand MySQL Foreign Keys, I hope someone can help.
I have two tables with the following structures;
reports
+-----------+------+-----------+------------------+
|id|user_id |status|report_type|request_id |
+-----------+------+-----------+------------------+
| |
+-------------------------------------------------+
users
+-----------+------+-----------+------------------+
|id|username|f_name|l_name |email |
+-----------+------+-----------+------------------+
| |
+-------------------------------------------------+
I have a foreign key in the reprots table (fk_uid) that links the user_id column in the reports table to the id in the users table.
I want to run a MySQL Query in PHP that will pull all the reports for a given user (either by username or user_id, not sure which is best?)
Thanks!
It could be done by simple inner join as
select
users.username,
users.f_name,
users.l_name ,
reports.status,reports.report_type,
reports.request_id from reports
inner join users on users.id = reports.user_id
If you want to search for a specific user then use where condition as
select
users.username,
users.f_name,
users.l_name ,
reports.status,
reports.report_type,
reports.request_id from reports
inner join users on users.id = reports.user_id
where users.username = 'some user name'
And if you want by userid then where condition is
where users.id = 'your user id'
if you only want to get reports and no user information then you can do a direct query to reports table if you have a user_id
Suppose you have userid in a variable in $userid
like
select * from reports where user_id = $userid
if you want data from both tables then you need to do a join like this
Reports for a specific user:
select r.*, u.* from reports r, users u where r.user_id = $userid
and r.user_id = u.id
Reports for all users
select r.*, u.* from reports r, users u where r.user_id = u.id
if you want to learn more about joins the visit this link
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
http://www.sitepoint.com/understanding-sql-joins-mysql-database/
I have a table of users, called ..wait for it... "users".. and then another table which records users' activity called ...you'll never guess... "activity"..
I have an HTML table which shows the users in the users table, but I want to order the table by the users' last activity.
Here's my query.
SELECT *
FROM `users`
JOIN `activity`
ON `activity`.`user_id` = `users`.`id`
ORDER BY `activity`.`timestamp`
LIMIT 25
The problem here is that it shows multiple rows for each user since there are multiple records for each user in the activity table.
How can I alter the query to only show one record for each user and order them by the last activity in the activity table.?
I did experiment with using the "DISTINCT" keyword but no luck :/
"one record for each user and order them by the last activity in the activity table.?"
Couly try this? Assuming you need both user info and activity info.
SELECT users.*, a.*
FROM users INNER JOIN (
SELECT user_id, MAX(timestamp) max_timestamp
FROM activity
GROUP BY user_id
) x ON users.user_id = x.user_id
INNER JOIN activity a ON x.user_id = a.user_id AND a.timestamp = x.max_timestamp
ORDER BY a.timestamp;
inner sub-query finds max_timestamp per user_id and outer JOIN finds activity has max_timestamp and user_id
This is not SQL solution, but what I would do is keep lastActivity column in Users table and update it when needed. Then sorting, filtering by this column would be very easy and efficient.
I want to have some help creating my query to get information from three different tables sharing information in common.
My first table is:
auctions
id title description user_id(who posted it)
My second table is:
bids
id user_id bid auction_id owner_id
My third table is:
users
id username X XX XXX XXXX
...and my SQL is as follows however it's not returning any results:
SELECT auction_bids.user_id AS applicant, auction_bids.*, auctions.title FROM auction_bids, auctions
WHERE auctions.user_id=".$_SESSION['userid']."
INNER JOIN users ON auction_bids.user_id = users.id
WHERE auction_bids.owner_id = ".$_SESSION['userid']."
What I need is to capture the auction's title, username who bidded on the auction and the bid. the auction has to have a bid and posted by the user who owns the $_SESSION['userid'].
Any help is appreciated.
You have two different 'where' statements, which may just need combining;
SELECT auction_bids.user_id AS applicant, auction_bids.*, auctions.title FROM auction_bids, auctions
INNER JOIN users ON auction_bids.user_id = users.id
WHERE auction_bids.owner_id = ".$_SESSION['userid']." AND auctions.user_id=".$_SESSION['userid']."
However, I'm not sure this is really what you want, as it will return only records where the specific user both 'owns' the item AND has bidded on it (both based on the userid session), rather than displaying all records from different people who have bidded on an item 'owned' by the user.
Something like: ?
SELECT auction_bids.user_id AS applicant, auction_bids.*, auctions.title FROM auction_bids, auctions
INNER JOIN users ON auction_bids.user_id = users.id,
WHERE auction.owner_id = ".$_SESSION['userid']."
Hope this points you in the right direction!
you have 2 where clauses, that is incorrect. I have revised your query based on your requirements.
SELECT auction_bids.user_id AS applicant, auction_bids.*, auctions.title
FROM auction_bids, auctions
INNER JOIN users ON auction_bids.owner_id = users.id
WHERE auction_bids.owner_id = ".$_SESSION['userid']."
AND auctions.user_id=auctions_bids.owner_id
I have tables as below.
Table Messages
message_id
parent_id
forum_id
user_id
Table Users
user_id
username
pass
How to query a user, and display the count of their messages with their username?
My query:
select count(subject), user_id from messages group by user_id;
The problem is that it only displays user_id and count of messages but no username. How do I make it display the name of the user?
You need to join the users table into your query:
select count(*), username
from messages, users
where users.user_id = messages.user_id
group by users.user_id;
SELECT username, COUNT(*) FROM messages M
JOIN users U USING (user_id)
GROUP BY U.user_id
This takes care of a scenario where two different user_id have the same username in the users table. Also this query is MySQL specific and may not work with other RDBMS.
I think this is the query you want.
$query = "SELECT u.username, COUNT(*) as total_count FROM messages m INNER JOIN users u ON m.user_id = u.user_id GROUP BY U.user_id"
Here total_count gives the total count of messages grouped by user id to avoid joining of same usernames.
Use below. I believe it will work.
SELECT messages.user_id, users.username, count(*)
FROM messages, users
WHERE messages.user_id = users.user_id
Good Luck!!!