Mysql left join multiple tables - php

I have a database which stores information about cars. There are 3 tables which don't have foreign keys:
make(make_ID, name,..)
colour(colour_ID,c_name,...)
engine(engine_ID,eng_size,...)
There's a 4th table which connects all the others together using foreign keys(FK):
carDetails(CarDetails_ID, CD_make_ID, CD_colorID,...)
Now I would like to use left joins to retrieve the appropriate information from tables about a car by using the IDs in the query.
Ideally I would like to select the make.name WHERE make.make_ID = carDetails.CD_make_ID etc. and keep joining extra information on the left. I have tried this and i'm getting errors on the 'where' clause...apparently it cannot be recognized.
SELECT name FROM make WHERE 'make.make_ID' = 'carDetails.CD_make_ID' LEFT JOIN colour.name WHERE colour.colour_ID = carDetails.CD_colour_ID
What am I doing wrong?

Try this
SELECT name FROM make, carDetails WHERE 'make.make_ID' = 'carDetails.CD_make_ID' LEFT JOIN colour.name ON colour.colour_ID = carDetails.CD_colour_ID

Related

join of two sql tables

I have two tables and I want to join them to get the desired output.
Say the 1st table (seat1) is
and the 2nd table (collegestudents) is
The desired output is
I have tried the below code. But it fails to give the desired result.
$rde2=mysqli_query($con, "select * from seat1 s
left JOIN collegestudents c ON c.Roll = s.Roll
");
Any help please.
You want a left join. Your query looks fine, but you would need not to use select *, and instead explictly list the columns that you want to select, using table prefixes. Otherwise, since you have a Roll column in both tables, a name clashes will happen, that your application apparently does not handle well.
select
s.Roll,
c.Name,
s.Subject
from seat1 s
left join collegestudents c on c.Roll = s.Roll

Joining 2 tables in PHP

This might be a dumb and very simple question, but I'm stuck and I've tried multiple ways already. So I have this code:
$RefLinksRAW = $GLOBALS['DATABASE']->query("
SELECT u.id, u.username FROM ".USERS." as u
LEFT JOIN ".TURNAMENT." as s
ON s.id_owner = u.id;");
But I want to also SELECT columns from TURNAMENT that corresponds to s.id_owner, how do I do that?
Basically I want to make a table that shows contents from both of those tables.
This is the TURNAMENT table, I want to make sure that 'id' from USERS table is same as 'id_owner' and also select 'wons' column
You can just add the columns you are after to the SELECT section. For example,
SELECT
u.id,
u.username,
s.somecolumn,
s.someothercolumn
FROM
users u
LEFT JOIN
turnament s ON s.id_owner = u.id;
Replace users and turnament with your actual table names, and somecolumn/someothercolumn with the columns you are after.

mysql select multiple tables and bring back all results

I am trying to select everything from multiple tables where there are 2 conditions.
But they only bring back a single result from the second table, instead of everything
Here is the MySql code
SELECT *
FROM `core_users`.`users`
LEFT JOIN `core`.`orders`
ON `core_users`.`users`.`uid` = `core`.`orders`.`uid`
LEFT JOIN `core_users`.`crm`
ON `core_users`.`users`.`uid` = `core_users`.`crm`.`uid`
WHERE (`core_users`.`users`.`signup_timestamp` BETWEEN '1476626400' AND '1476712800'
OR (`core_users`.`crm`.`type` = 'refresh_5in5' AND `core_users`.`crm`.`value` BETWEEN '1476626400' AND '1476712800') )
This just brings back 1 result from the crm table. However I want it to bring back all the results from the crm table.
How do I bring back everything from users, orders, and crm while having a WHERE clause on both tables?
Try select users.*, orders.*, crm.*
Please note that, in such case if you have common columns in any of these table like id in every table it would cause an ambiguous column name error. To get rid of that you need to specify these with alias name users.id as user_id, orders.id as order_id, crm.id as crm_id and so on.

php and mysql queries

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

retrieve data from 4 tables

I have these four table assume that ring table consist of five fields(jewelry_id,ring_id,image,type,brand_id)
Note that brand table have its foreign key in ring table and ring and style both have foreign keys in ring_style table. now i want to retrieve the following data from these four table
(ring_id, image,type, brand,style) but did't get the query any help will be greatly appreciated.
You just join each table using the columns that relate them.
SELECT
ring.ring_id,
style.image,
ring.type,
brand.brand,
style.style
FROM
brand
INNER JOIN
ring
ON
brand.brand_id = ring.brand_id
INNER JOIN
ring_style
ON
ring.jewelry_id = ring_style.jewelry_id
INNER JOIN
style
ON
ring_style.style_id = style.style_id
SELECT
ring_id, image, type,
brand,
style
FROM ring
LEFT JOIN brand ON ring.brand_id = brand.brand_id
LEFT JOIN ring_style ON ring.jewelry_id = ring_style.jewelry_id
LEFT JOIN style ON ring_style.style_id = style.style_id
Note that each ring will appear one or more times. It will appear more than one time when there is more than one ring_style record for the ring.
You have to use the SQL command JOIN:
An example to get the information from ring and brand with one query use:
SELECT * FROM ring
JOIN brand ON ring.brand_id = brand.brand_id;
WHERE ring.jewelry_id = 123456
Use multiple JOIN in one query to get multiple tables connected in one query.

Categories