how to make the link between my 2 sql tables? - php

I cannot correctly make the link between my 2 tables, I have a table that represents my users and another with appointments but I would like to make the link between the 2 to be able to display the appointments users.
My reservation table looks like this :
[tablebooking]
My user table looks like this :
[tableusers]
What I Tried :
[test]

If your appointment functionality is one to one relation like one appointment will have only one member then you can use below query(query followed fields from your screenshots)
SELECT
M.*, B.*
FROM
members AS M
INNER JOIN bookings AS B ON B.userid = M.userid
OR
if you want to have more than one members in one appointment, you need to have tables as below
[YOUR_USERS_TABLE_NAME] => Users list
[YOUR_BOOKINGS_TABLE_NAME] => List of appointments created
[YOUR_BOOKING_MEMBERS_TABLE_NAME] => It should have list of appointment id and members id(one(appointment) to many(users) relation)
and query will be like below
SELECT
B.*, U.*
FROM
[YOUR_BOOKINGS_TABLE_NAME] AS B
INNER JOIN [YOUR_BOOKING_MEMBERS_TABLE_NAME] AS M ON M.bookingid = B.id
INNER JOIN [YOUR_USERS_TABLE_NAME] AS U on M.userid = U.id
GROUP BY B.id, U.id
Hope my answer will help you.

Please add user_id in your bookings table and then run below query in your phpmyadmin.
select * from bookings as b INNER JOIN users as u ON u.id = b.user_id
Thanks.

You probably must use an external table to connect this tables.
EX:
foo{
id: primary,
idTableUser: external key,
idTableBooking: external key
}

Related

How to JOIN 4 table

Currently I have 4 tables. activity, category, pic and useraccount.
In this system, there's 2 user: applicant and person-in-charge. applicant will login and request for activity (which will be stored in table activity).
then depend on the category, person-in-charge will be assigned(will be stored in table pic with their name and activityID)
activity: `activityID`(PK), `username`, `categoryID`
category: `categoryID`(PK), `categoryName`
pic: `activityID`, `personincharge` (name of the pic)
useraccount: `name`, `username`
note: personincharge in table pic and name in table useraccount has the same value.
note 2: I will join table category and activity using categoryID to display the categoryname.
My system is going to have user log in by using username. I want to display table of activity record of the person in charge, which means it will only shows activity that the user(the one who logged in) is in charge of.
Currently I am only able to join table category and activity and display it. Here's what I have:
$sql = "
SELECT *
FROM activity a
JOIN category c
ON a.categoryID = c.categoryID
WHERE a.username= '".$_SESSION['username']."'";
Gone through so many trial and error but still cant find the answer. Thanks for the help in advance.
the flow of my system:
situation 1:
user login -> user request activity -> admin will pick the person in charge of the activity (the name of the pic will be insert into pic table along with the activityID)
situation 2:
pic login -> pic see the list of activity that he/she has been assigned to be in charge of and take action on the request.
**the situation 2 is what i'm currently stuck on. to display only activity he/she is assigned to.
If you are doing inner joins, then you can also join using the where clause, like so:
SELECT *
FROM activity as a,
category as c,
pic as p,
useraccount as u
WHERE a.categoryID = c.categoryID,
a.activityID = p.activityID,
p.personincharge = u.name
One:
Do not name your tables after smart shortcuts. Think what you are optimizing, and what you are loosing! Use "table AS t" shortcuts in actual queries.
Two:
Your table personsincharge contain the people responsible. Do a join with it. accountname is requester account. Once you join presonsincharge you have access to handlers
Three:
DO NOT USE POST VALUES TO CONCATENATE SQL STRINGS
You could have multiple joins in the same query like this:
$sql = "SELECT * FROM activity a
INNER JOIN category c ON a.categoryID=c.categoryID
INNER JOIN table_name as table_alias ON table_alias.ID = another_alias.ID
INNER JOIN ...
INNER JOIN ........
INNER JOIN ......
WHERE a.username= '".$_SESSION['username']."'";
Try this
select a.activityID, b.categoryName, c.personincharge, d.name
FROM activity a
LEFT JOIN category b ON b.categoryID = a.categoryID
LEFT JOIN pic c ON c.activityID = a.activityID
LEFT JOIN useraccount d ON d.username = a.username
WHERE d.username= '".$_SESSION['username']."'";

Want to select data from different tables

I have two tables in database, named users(store user details) and posts(store post details). Now i want to get data from both tables. Like user_image from users and post description from post.
I am using this query
SELECT * FROM `users`AS u,`posts` WHERE u.user_id IN (SELECT user_id FROM `posts`)
But it returns duplicate data. I have 2 users and 3 posts but it returns 6 posts.
Try something like:
Select a.user_image, b.post_description from users as a join posts as b on a.user_id = b.user_id
Do an inner join & you shall get the desired result
In the above query a & b are alias for the two different tables. I you do not want to use alias you can also write it as users.user_image in your select statement.
Write the fields you want from both the tables in your select statements.
The below image will help you understand the inner join
Inner Join Circle for understanding
Use group by as below:
SELECT * FROM `users`AS u,`posts` WHERE u.user_id IN (SELECT user_id FROM `posts`) group by u.user_id
What about
Select * FROM user u right join posts p on u.id = p.user_id
?
if you want to get data from both tables you need to use joins.and you make sure the two tables are interlinked by primary keys
so use this can help
select user_image,post_description from users join posts on users.user_id=posts.user_id;

How can I write this SQL query? Displaying friends

I am developing a friend system, which handles friend requests and friendships.
I have a table named Member where I keep information about each member including their id, first and last name
and another table named Friendships where I keep track of the id of the first friend Friend1 and the second Friend2 and the date of the friendship formation.(Note: each request is recorded twice in my database; ex 1 -> 2 and 2->1 )
I am trying to write a query to display the first and last name of the friends of the current user. I know I have to INNER JOIN both tables, but I am not sure ON what exactly.
Assuming the friend table and members table have same ID columns,
SELECT m.first, m.second
FROM m.member INNER JOIN f.friendships
ON f.id = m.id;
Could be somthings like this
select a.firstname, b.lastname from Member a
inner join Friendships b on (b.friend1_id = a.id )
and a.id = 'current_user_id'
On the foreign keys (I guess Members.Id=Friendships.Friend1) but it really depends on the semantics you are implementing with those duplicate (bidirectional?) friendship relations.

Inner Joining with two tables

I need a little help setting up my query. I'm simply trying to access the amount of people who are in the same 'clan' by joining these two tables together, clan, users. Each users has a column 'clan' which is the same as the table clan's column 'roomOwner' and then I'm trying to get the table clan's information along with the amount of members so it would be like: room, roomOwner, members
So basically all I have is this:
SELECT c.*, count(u.clan) AS members FROM clans c inner join users u WHERE c.roomOwner = u.clan ORDER BY members;
It only shows one clan though. Any help please?
Your query has no GROUP BY clause. and I think it's only returning single record right? LEFT JOIN is needed here since there are possibilities that a clan has no member.
SELECT b.roomOwner, COUNT(a.clan) memberCount
FROM clan b
LEFT JOIN users a
ON a.clan = b.roomOwner
GROUP BY b.roomOwner
ORDER BY memberCount
You forgot GROUP BY. Do you have some "id" column in "clans" table? Group by that "id"
SELECT c.*, count(u.clan) AS members
FROM clans c
inner join users u ON c.roomOwner = u.clan
GROUP BY clans.id
And you need LEFT JOIN there instead of INNER JOIN if you want to see info about all clans, even having 0 users.
Perhaps this will help:
select c.*, count(links.id) as members
from clans c
left join users u on c.roomOwner = u,clan
group by u.clan
order by members

User/Subscriber sql query

I'm creating a table that shows all the registered users to which the current user has not yet subscribed to. But once he has subscribed someone, I need to filter that list to exclude those.
Let's say the theres a table called subscribed which lists the User and to whom he is subscribed to.
|UserId||SubscriberID|
Its easy to make it into multiple queries, but I've been unsuccessfully trying to make it into one query, to save an extra loop of MySQL calls.
Here's What I have so far:
SELECT u.UserID, FullName, UserName from users u
LEFT JOIN subscribed t ON
((u.UserName LIKE '%$search%' OR
u.Email LIKE '%$search%') AND
({$_SESSION['ID']} = t.UserID
AND t.FollowerID != u.UserID)
)
I know the last part of the query is wrong, since I only compare if the UserID and the FollowerID don't match for one particular row, not the entire table.
To find a list of results in table A that are not in table B, you have two options. Either use the NOT IN syntax, or LEFT JOIN and look where the PK field in the B table is NULL.
NOT IN example:
SELECT a.id FROM a WHERE a.id NOT IN (SELECT b.id FROM b)
LEFT JOIN example:
SELECT a.id
FROM a
LEFT JOIN b ON (a.id = b.id)
WHERE (b.id IS NULL)

Categories