I have three different tables I am trying to pull info from. Related in different ways. I am trying to find a Join statement that will allow me to pull the information a little better than using the two queries I am now.
Here is what I have
SELECT * FROM reports LEFT JOIN students USING (sID)
This allows me to properly merge the two tables. However - There is a row in the reports table named pID. I have another table named test, that also has a matching pID row, along with other data. I am trying to access the data from that table as well. Is it possible to join three tables in that way? Not only getting data from the sID, but the pID as well
You can do the following
SELECT *
FROM reports
LEFT JOIN students USING (sID)
LEFT JOIN table3 USING (pID)
Where table3 is the name of your table.
Hi robert yes it is posible. You can do a query like as shown below given your three tables, reports, students, and test.
SELECT *
FROM `reports`
LEFT JOIN `students` ON `students.sid=reports.sid`
LEFT JOIN `test` ON `reports.pid=tests.pid`
Related
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 :)
I'm creating a website where the user can add some information about multiple computers into a database.
In the mysql database I have 3 tables. TypeOfMachine, Software and Hardware. The only common thing they have is the NumberOfMachine.
I must create a page where the user can run reports showing devices that have a specific piece of software installed (user specified) or specific hardware (user specified) after he submitted the computer's info.
Any ideas how I can do this?
And how I can connect all 3 tables into one?
I thought about this code but i dont know what to put in WHERE. I have 10 variables. and I have to show all the computers with what the user has asked and their info as well!
$search1 = "
SELECT
TypeOfMachine.NumberOfMachine, TypeOfMachine.TypeOfMachine, TypeOfMachine.OS, Software.Software1, Software.Software2, Software.Software3, Hardware.SSD, Hardware.Harddisk, Hardware.MonitorSize, Hardware.Ram, Hardware.Rom, Hardware.Processor
FROM TypeOfMachine, Software, Hardware
WHERE
but i
You want to use a join. This example is based on the fact that you've said the NumberOfMachine field is present in all tables and is a common link between them:
SELECT
TypeOfMachine.NumberOfMachine,
TypeOfMachine.TypeOfMachine,
TypeOfMachine.OS,
Software.Software1,
Software.Software2,
Software.Software3,
Hardware.SSD,
Hardware.Harddisk,
Hardware.MonitorSize,
Hardware.Ram,
Hardware.Rom,
Hardware.Processor
FROM TypeOfMachine
LEFT JOIN Software
ON Software.NumberOfMachine = TypeOfMachine.NumberOfMachine
LEFT JOIN Hardware
ON Hardware.NumberOfMachine = TypeOfMachine.NumberOfMachine
WHERE
...
It's general question, I don't know which tables contains a spesific columns as indicator for all tables. It's about inner and outer join:
The two common types of joins are an inner join and an outer join. The difference between an inner and outer join is in the number of rows included in the results table.
Inner join: The results table produced by an inner join contains only rows that existed in both tables.
Outer join: The combined table produced by an outer join contains all rows that existed in one table with blanks in the columns for the rows that did not exist in the second table.
For instance, if table1 contains a row for Joe and a row for Sally, and table2 contains only a row for Sally, an inner join would contain only one row: the row for Sally. However, an outer join would contain two rows — a row for Joe and a row for Sally — even though the row for Joe would have a blank field for weight.
The results table for the outer join contains all the rows for one table. If any of the rows for that table don’t exist in the second table, the columns for the second table are empty. Clearly, the contents of the results table are determined by which table contributes all its rows, requiring the second table to match it.
Two kinds of outer joins control which table sets the rows and which must match: a LEFT JOIN and a RIGHT JOIN.
You use different SELECT queries for an inner join and the two types of outer joins. The following query is an inner join:
SELECT columnnamelist FROM table1,table2
WHERE table1.col2 = table2.col2
And these queries are outer joins:
SELECT columnnamelist FROM table1 LEFT JOIN table2
ON table1.col1=table2.col2
SELECT columnnamelist FROM table1 RIGHT JOIN table2
ON table1.col1=table2.col2
In all three queries, table1 and table2 are the tables to be joined. You can join more than two tables. In both queries, col1 and col2 are the names of the columns being matched to join the tables. The tables are matched based on the data in these columns. These two columns can have the same name or different names, but they must contain the same type of data.
For general concept you can use #Scrowler suggestion, or this one:
http://stackoverflow.com/questions/1204217/mysql-select-join-3-tables
I have four tables in a database now asking how can I search the four tables using a single search parameter with PHPMYSQL and display the result in a single page?
thanks alot friends.
Select *
From table1 inner join table2 on table1.col=table2.col
inner join table3 on table#.col=table3.col ..........
where Col=?
you can replace on ?=? with "using (column)" if both tables have the same column name.
MySQL - Workbench (PHP):
Tables:
TUsers (One to many relationship with TCompanies):
TUsers_CompanyID (FOREIGN KEY)
TUsers_UserName
TUsers_UserPassword
TUsers_ID (UNIQUE)
TCompanies:
TCompanies_CompanyName
TCompanies_CompanyContactNumber
TCompanies_CompanyAddress
TCompanies_ID (UNIQUE)
Is it possible to link multiple tables in a relational database without using the JOIN, or INNER JOIN query commands, without duplicating data in tables?
Thus speaking even another way of creating a relationship that makes the one table "point" to the other's data.
So that one can query the following and successfully retrieve all the data from both tables at once:
MySQL:SELECT * FROM TUsers;
See example above..
You can do it without "appearing" to use a join (ie, the word JOIN won't be in the query), but MySQL will still perform a JOIN...
SELECT *
FROM TUsers, TCompanies
WHERE TUsers_CompanyID=TCompanies_ID;
You won't be able to escape having to use a JOIN for how you want to display your data, but what you might want to do is create what's known as a VIEW, so that you don't actually have to type out the JOIN commands whenever you want to query for the user data:
CREATE VIEW UsersView AS
SELECT *
FROM TUsers a
INNER JOIN TCompanies b ON a.TUsers_CompanyID = b.TCompanies_ID
Then once the view is defined, you can just select from UsersView like so:
SELECT * FROM UsersView
...And it will return the users information as well as the joined company information. You can think of views as a way to simplify (or "compactify") more complex queries, because underneath the hood, it's actually the same thing as:
SELECT *
FROM
(
SELECT *
FROM TUsers a
INNER JOIN TCompanies b ON a.TUsers_CompanyID = b.TCompanies_ID
) UsersView
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