join two sql table and and set session using the table - php

I am trying to create a booking system, I have a table for user to log in, another table for advocate login and another table for booking information. I want data entered from user to be displayed to a specific advocate. I have tried using SQL JOIN but it is not displaying any information.
MY TABLES
advocate
bookingcalendar
here is the SQL code
$sql = ("SELECT bookingcalendar.*,advocate.userName, FROM bookingcalendar JOIN advocate ON bookingcalendar.*=advocate.userName WHERE $current_epoch BETWEEN start_day AND end_day");
$result = mysqli_query($conn, $sql);

bookingcalendar.*=advocate.userName part is incorrect use bookingcalendar.Column_name where Column_name is common on both the table bookingcalendar and advocate which may be userName in your case
SELECT bookingcalendar.*,advocate.userName
FROM bookingcalendar JOIN advocate
ON bookingcalendar.userName = advocate.userName
WHERE $current_epoch BETWEEN start_day AND end_day

You need to use a field that is in common between your two tables as the key to the join condition. For example, with some kind of userID :
SELECT bookingcalendar.*, advocate.userName
FROM bookingcalendar
JOIN advocate ON bookingcalendar.userID=advocate.userID
WHERE $current_epoch BETWEEN start_day AND end_day
I advise you to not use the userName as the key to join your two tables, because multiple users can eventually have the same userName, and the join on two strings takes more time than the join on two numbers.
That is not just an advice to take lightly. You absolutely should join your tables on some numerical ID instead of a user name.
-- EDIT --
Actually, I don't really know what an "advocate" is, so maybe your join key shouldn't even be some user id/name. To know that, I need to better understand the relationship between advocate, user and bookingCalendar. Maybe a description of their columns could help us answer this hidden part of the question.

Related

Multi-Table, Multi-WHERE and SELECT MySQL query

So, I am trying to select some data from 4 tables using a query I have attempted to throw together.
SELECT *
FROM cards
LEFT JOIN cards_viewers ON cards.card_id = cards_viewers.card_id
(SELECT *
FROM folders
WHERE folder_id = cards.card_folderID)
(SELECT user_firstName,
user_lastName,
user_avatar
FROM user_data
WHERE user_id = cards_viewers.user_id)
WHERE cards_viewers.user_id = '.$u_id.'
ORDER BY cards.card_lastUpdated DESC
Basically, the query selects data from the four tables depending on the user_id in table user_data. I have attempted to initially fetch all data from the tables cards, and cards_viewers, and have went on to use this data to select values from the other tables (user_data and folders).
The query is wrong, I know that. I have learnt the majority of basic MySQL, but I am still struggling with more complex queries like the one I am trying to write now. What query can I use to select the data I want?
Links to any documentation to parts of queries would prove very useful in helping me learn how to create queries in future, rather than just relying on StackOverflow.
Many thanks.
You don't need "MULTI-WHERE" but multiple joins, you just need to keep doing joins until you get the tables you need.
Here's an example:
SELECT *
FROM cards LEFT JOIN cards_viewers
ON cards.card_id = cards_viewers.card_id
LEFT JOIN folders
ON folders.folder_id = cards.card_folderID
LEFT JOIN user_data
ON user_id = cards_viewers.user_id
WHERE cards_viewers.user_id = '.$u_id.'
ORDER BY cards.card_lastUpdated DESC
To custom the fields you want to get just change * for the name of the field being careful about ambiguous column naming.
For further information check MySql Joins. Hope this helped you :)

Get rows from a MySQL database where the number of foreign keys are variable.

A subset of my database contains 3 tables
users
user_details
tasks
The main column of the user table is:
id {PK}
The user details table contains information about the users (only some users have these details, students). Thus the main columns of the users details table are:
user_id{PK,FK} | supervisor_id {FK}
The supervisor_id is an id in the users table. Each student has one supervisor.
Lastly, there is the tasks table where only students create tasks and the main columns of the tasks table are:
task_id{PK} | user_id{FK}
The problem I am having is getting a proper query for, if a supervisor wants to see all his students tasks. I know you can query all the students in the user_details table who have the supervisor's id. Then create another query where you select all the tasks whose user_id matches that of the first query performed.
This does not seem like a very efficient was to go about achieving this result. Are there better alternatives?
select ud.supervisor_id, ud.user_id, t.task_id
from user_details ud, users u
where ud.user_id = t.user_id
What you are looking for is a join. Instead of writing two separate queries to get the information, a join will allow you to connect the tables that you have in one query and get the information you need much faster.
Select *
From user_details ud
join tasks t
on ud.user_id = t.user_id
Where ud.supervisor_id = ?
The join essentially allows you to create one big table out of all of the columns of the tables you are using. The on keyword tells sql which values go together, so that you know all of the tasks belong to the student whose id matches the id that the supervisor has. Then, you can select whatever columns you like out of either table (as well as a lot of other fancy things).

Calling information from one table based on many-to-many table

I'm a bit stuck on using the INNER JOIN concept with mySQL & PHP to call information from two tables. I would consider myself an upper-level beginner in PHP & mySQL, using Dreamweaver to lean on from time to time as well.
What I have is a "Accounts" table with a primary key account_id which is stored to a session when a user is logged in. I have a m2m table which stores account_id and it's production_id. And lastly I have a Productions table with a primary key of (you guessed it) production_id. What I'm trying to do is have it look up all the productions a user is assigned to (via the m2m table) and pull the details for those specific productions, from the productions table itself. I'm assuming using INNER JOIN would be the way to go about this?
Here's what I was trying to execute (note that "sessionaccountid" is setup on Dreamweaver's end to pull the session data already).
SELECT *
FROM Productions
JOIN Accounts Productions Junction on account_id = sessionaccountid
Unfortunately, this is turning up a Not unique table error in mySQL. Any guidance on getting this to lock in right would be appreciated! And of course if you could also explain what I did wrong/how your suggestion makes more sense, so I can write it down for myself, that would also be great!
As it is your current query doesn't follow nether implicit nor explicit JOIN syntax and therefore is incorrect.
Use explicit ANSI JOIN syntax and specify how your tables are related in your query
SELECT a.account_id, a.account_name, p.production_id, p.production_name
FROM Junction j JOIN Accounts a
ON j.account_id = a.account_id JOIN Productions p
ON j.production_id = p.production_id
WHERE j.account_id = ?
Here is SQLFiddle demo

Need help grabbing all the data from two tables in SQL

I have a form set up where the user can enter in the username or lastname of a person. I then take either the username or lastname depending on what they entered and query the database and displaying all the userinformation and whether they have access to certain areas. There are two tables I need to query and get all the information for the user entered. I'm assuming you use a JOIN but I'm not quite sure how to do it. Right now I have the query to grab all the data from the users table I just need help adding in the second table. The two tables look like this
Users {
uid
username
password
firstname
lastname
email
}
userAccess {
uid
assessment
development
learningmodule
project
}
The SQL I have so far is
SELECT *
FROM Users
WHERE username = '$username' OR lastname = '$lastname'
You are right you'll need to use a join. This type of join is called an Inner Join. You can achieve it like this:
SELECT * from Users, userAccess WHERE Users.uid = userAccess.uid AND Users.uid = #;
I'm guessing you'll have an id column that is the Primary Key for your userAccess table as well. The above query will provide the data from both tables in 1 record for every condition where the above statement is true.
If you are using MySQL you can read more about joins here:
http://dev.mysql.com/doc/refman/5.0/en/join.html
You donĀ“t mention which database you are using, although I'm guessing MySql.
What you need to do is a simple inner join:
SELECT u.uid
u.username,
u.password,
u.firstname,
u.lastname,
u.email,
ua.assessment,
ua.development,
ua.learningmodule,
ua.project
FROM Users u
INNER JOIN UserAccess ua
ON u.uid = ua.uid
WHERE u.firstname LIKE '%$username%' OR u.lastname LIKE '%$lastname%';
Do note that I'm not selecting the uid again in the UserAccess table as it is redundant since you already selected it from the Users table and since you are joining by that key you don't need it again.
Also note that instead of using the equal operator (=) I use the LIKE operator since there are more chances that you want to filter information based on partial info such as keywords and not full words.
I'd recommend you to have a look at a SQL tutorial for beginners. This one seems to be good enough.
I'd also recommend you to read the guidelines on how to post questions here or else you won't get much attention from anybody, plus they will downvote you :)

Mysql join query retrieving profile images of users with ids from other table

I'm having trouble with a join query, my issue is as follows.
Table: battles
Fields: id,attacker_id,defender_id
Table: users
Fields: id,profile_image
I would like to do a query to retrieve a battle and get the profile images as well from the other table.
Is there a way to do this in a single or do I have to do more than one?
Thanks in advance.
I wanted to wait a while to see if you had any attempt or if you will answer my first question to know if I understood the problem. But maybe you don't have a starting point. Try something like:
SELECT
a.profile_image as attacker_profile_image,
d.profile_image as defender_profile_image
FROM
`battles` b
LEFT JOIN
`users` a
ON
b.`attacker_id` = a.`id`
LEFT JOIN
`users` d
ON
b.`defender_id` = d.`id`
the problem here is the fact that you need to join with the users table twice, so you will need to create aliases for the columns you plan to use
This query will fetch the two images only, you will need to add the extra fields

Categories