Getting two values from same Joined table - php

I have two tables:
procedures
countries
Table procedures has two columns
destination_country_iso
origin_country_iso
Table countries
iso
name
Im trying to join up the two tables in order to get both destination an origin country name from countries table.
SELECT *
FROM draft_procedures AS drp
LEFT JOIN countries AS c1 ON drp.destination_country_iso = c1.iso
LEFT JOIN countries AS c2 ON drp.origin_country_iso = c2.iso
The SQL result display proper result, but I now have two duplicate columns "name" and Im unable to retrieve them with the php code:
$destination_country=$row['c1.name'];

access it like this
SELECT drp.*,c1.name as c1name,c2.name as c2name
then
$row['c1name'] $row['c2name']

Related

SQL Query Phpmyadmin is not giving correct results having more than 3 tables with WHERE Clause

I am having relational database and trying to execute following query up to 3 tables It is giving me correct results i.e. 248 records of students in institute id 910, but when I try to make a query having more than 3 tables it gives me 19000+ results.
SELECT *
FROM Student,StudentRegistration,RefStudentType,RefGender,SubjectCategory
WHERE Student.student_id=StudentRegistration.student_student_id
AND StudentRegistration.reg_student_type_std_type_id = RefStudentType.std_type_id
AND Student.student_gender_gender_id = RefGender.gender_id
AND StudentRegistration.reg_student_subjectCat_sub_cat_id=SubjectCategory.sub_cat_id
AND Student.student_institute_inst_id=910;
`
Tried with JOIN as well but same 19000+ records incorrect results
SELECT * FROM Student INNER JOIN StudentRegistration ON student_id=student_student_id INNER JOIN RefReligion ON RefReligion.religion_id=Student.student_religion_religion_id INNER JOIN RefStudentType ON RefStudentType.std_type_id=StudentRegistration.reg_student_type_std_type_id WHERE student_institute_inst_id=910;
Any solution, query logical errors or something new
I think this is due to having several records for one data. For example, there might be several records in the SubjectCategory table for id = '910'
It is best to use left/right/inner/outer joins without using from tbl1, tbl2
What I suggest is to check the tables one by one with the id.

Mysql left join multiple tables

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

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

SQL Count from Two Tables with Inner Join from Third Table

My database has two similar tables with different names. One of the columns contained in both tables is called "zips", which contains a zip code.
I have a different table (US zip code table) where each entry contains a county name, county ID, state, state ID, and zip code.
I need a query that will tell me how many entries in the first two tables match a given county ID, and will group the results by county.
That is, lets say table A has 4 entries for zip code '30017', and table B has 1 entry for '30017'. In the US zip code table, '30017' corresponds to the county name "Gwinnett" and the county ID '839'. When I run the query the desired result is:
Gwinnett 5
Here is the latest query I've tried. I have no idea what numbers it's returning. It's definitely not what I'm expecting, as I've tried the individual component queries to test the results.
SELECT b.County, COUNT(*) as Calls FROM (aLeads a, pLeads p)
INNER JOIN zipCodes b ON a.zip = b.ZipCode
WHERE b.countyID IN (2897, 2146, 839)
AND a.callDate BETWEEN '2013-10-01' AND '2013-11-30'
GROUP BY b.County
Any ideas?
Try starting with the zipcode table, and joining it with aLeads and pLeads:
SELECT b.County, COUNT(*) as Calls
FROM zipCodes b
INNER JOIN aLeads a ON a.zip = b.ZipCode
INNER JOIN pLeads p ON p.zip = b.ZipCode
WHERE b.countyID IN (2897, 2146, 839)
GROUP BY b.County

Searching multiple database table with a single search parameter

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.

Categories