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/
Related
$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
I have 2 tables: "users" and "pages".
I need to list table "users" but IF user have own pages to first display users who have "pages" and after that to list others who don't have "pages".
USERS table:
id | name | lastname
PAGES table:
id | user_id | content
I don't know how to write query for this.
Thanks!
Something like this should get you close:
select users.id as user_id,
users.name as user_name,
users.lastname as user_lastname,
count(*) as tot
from users
left join pages
on pages.user_id = users.id
group by user_id, user_name, user_lastname
order by tot DESC, user_lastname, user_name
It should list the users with the most pages first, followed by the users with fewer pages, followed by the users with no pages, and within each page count, order the users alphabetically by last name and then user name.
You can try LEFT JOIN, this will show all users and replace page data by NULL if the user doesn't owns any, then sort by page. The distinct aims to avoid doubloons.
SELECT DISTINCT u.name, u.lastname FROM users u LEFT JOIN pages p ON u.id = p.user_id ORDER BY p.id;
Join both tables together then sort on pages.user_id
select *
from users u
left join pages p on u.id = p.user_id
order by p.user_id desc
You can easily add a column in your query with COUNT, so you know how many pages the user have.
SELECT u.id, u.name, u.lastname, count(p.id)
FROM USERS u
RIGHT JOIN PAGES p ON u.id = p.user_id
GROUP BY u.id
I have two mysql tables members_tbl and post_tbl
members_tbl:
id|userName |fname |lname |friendArray
post_tbl:
postId| memId | thePost |postDate
now, I'm trying to display post from user id and from his friendArray.
please let me know how to do it (still new to php)
Since MySQL lacks an explode function, you either need to create a relation table and use joins, or use multiple queries with php processing inbetween. I strongly recommend the relational approach as it conforms to database standards (normalization) much more than the alternative and is easier to implement.
You need a third table, which describes the relation between two friends, I.E.
friends_tbl
user1_id | user2_id
With a primary key on user1_id and user2_id (thereby preventing duplicates). For every friend relationship, I.E. user 1 is friends with user 2, there is one row in this table. You can then get the listing you want with the following query.
SELECT p.*, u.*
FROM posts_tbl p
INNER JOIN members_tbl u
ON u.id = p.memId
WHERE u.id IN (
SELECT user2_id AS id
FROM friends_tbl
INNER JOIN members_tbl
ON (user1_id = id)
WHERE members_tbl.id = $id
UNION
SELECT user1_id AS id
FROM friends_tbl
INNER JOIN members_tbl
ON (user2_id = id)
WHERE members_tbl.id = $id
)
ORDER BY p.postDate
SQLFiddle of the above.
create a different table for your friend-relations and then join this table in your SQL.
I have a table in which I store followers, I have another table in which I store friendships
Now I have third table which stores stream data.
Its a social network, there are many reasons so I don't wish to have one table for follower & friendships (Means facebook subscriptions/friends)
Can someone presents a way how should I query streams table to pick activities of both friends & followings ?
Any help would be really appreciated, thank you
Here is simple Database Scheme, its not really like this but almost!
Okay here is database tables schema please,
Followers table.
Row_ID
User_ID
Following_User_ID
Friends Table
Row_ID
User_ID
Friend_ID
Stream Table
Row_ID
User_ID
Contents_ID
Time
Type
What are you looking for is probably best done as two distinct results sets... or a union of the two.
Select "friend" as src, author, post from friends f inner join streams s on s.author = f.id
union
Select "follower" as src, author, post from followers f inner join streams s on s.author = f.id
This is just some pseudo coding but it should give you an idea of how to proceed. Without knowing your database schema, this is the best I can offer.
Edit:
This might be what your looking for then
select user_id, contents_id, time from (
select user_id, contents_id, time
from followers f inner join stream s on s.user_id = f.user_id and f.user_id = "username"
union
select user_id, contents_id, time
from friends f inner join stream s on s.user_id = f.user_id and f.user_id = "username"
) order by time desc
This will return the data in time order, descending.
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!!!