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
Related
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.
I am currently making an android app which uses a feed to display statuses made by users. I have three tables within the same database, each has username either as a primary or unique key column, but each table has different information relating to that user.
For instance, the first table ===>> tbl_users:
username
fname (first name)
mname (middle name)
lname (last name)
etc. (the list is long)
The second table ===>> tbl_userprofilepictures:
profilepictureID
username
profilepicturepath
The third table ===>> tbl_user_feed:
postID (the status' unique ID)
username
status
imagepostpath (the path to the image uploaded with the status)
timestamp
I want to be able to search for the username across all three tables and display the relevant information relating to them on their post. For example I will need their name and surname for tbl_users and I will need their profilepicturepath for tbl_userprofilepictures as well as their status, imagepostpath and timestamp from tbl_user_feed.
Would I need to do this in a seperate PHP file or in the app itself? PS I'm fairly noob at PHP so please feel free to help a bro out.
May the force be with you.
You can use JOIN.
What is JOIN ?
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
Source : w3schools.com Joins
Here is the sample I made base on your tables given. For these one our common field is username.
SELECT CONCAT('a.fname', 'a.lname'), b.profilepictureID, c.status, c.imagepostpath, c.timestamp
FROM
tbl_users as a
LEFT JOIN tbl_userprofilepictures as b ON b.username = a.username
LEFT JOIN tbl_user_feed as c ON c.username = a.username
Using Alias (table_name as custom_name) is a good practice in joining the tables
I have a database with users. Now I'm trying to create a wall (Facebook like).
I've added a table in my db called status_update and added a row username(user who posted the status). Then I created a query to search for statuses from the user on whose profile page you're on (select from status where username=$_GET['profile']).
I'm wondering how smart this is? Is there a way to use JOIN or something? Now this was the only logic solution that came to my mind and I know how to make it.
Also this is fine for this page but when I'll create a feed page for those who are "Friends or Followers" that's gonna be a bit tricky.
Is there a better solution for this?
yes you can join two tables by there index or any value, just do it like this
select * from table1 inner join table2 on table.field=table.field
it depends what join you want, inner join if data present on both table other wise left if data may or may not present
I have looked at other questions/answer regarding my question, but for some reason, every time I try to implement an answer exactly how they implemented it, it throws an error. But I am paginating some things now, and I need to order my results by column in another table.
Here is the code I have now:
SELECT `id`,`name`,`players`,`max_players`,`status`,`host`,`port`
FROM `servers`
LIMIT :to,:from
The table it is getting stuff from is the servers table, and it needs to get the column rank from the server_profiles table, and order it by that. I try to understand how MySQL joins work, but they always seem to confuse me, from looking at examples, to reading the markup on the MySQL wiki.
You can use join for that. For that you need to have a relation between both tables. I have used server.ID = server_profiles.serversID for example.
SELECT s.* FROM servers s
JOIN server_profiles sp
ON s.ID = sp.serversID
ORDER BY sp.rank
LIMIT :to,:from
See this SQLFiddle
I got 2 tables for example a USER TABLE with id as primary key(auto-increment)
and a BOOKING TABLE with userid as foreign key (integer) of id in user table.
Using phpmyadmin mysql database.
How do i write a php script where i can link this two fields together so that they are always related?
So when the user enters name and books a tour on the web form, it does everything automatically on the backend.
I am new to php and mysql and searched so many forums and watch tutorials but still dont have a clue.
Please help!
What you are currently looking is how to join multiple tables. The query below uses an INNER JOIN which displays only records which has matches on both tables.
SELECT a.*, b.*
FROM User a
INNER JOIN Booking b
ON a.ID = b.userID
// WHERE ..conditions here..
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins