How use two Foreign Keys inside Single query? - php

I have 2 tables on database,'locations' & 'sales_person'.inside the 'location' table there are two columns name "location_from" & "location_to".Those 2 tables include primary keys of sales_person table as Foreign Keys.
the problem is, how to get both sales person names from sales_person table using only one query?

Join sales_person table twice with location to get names for location_to and location_from
select t.name,f.name
from location l
join sales_person t on l.location_to = t.id
join sales_person f on l.location_from = f.id
I assume you have name column in your sales_person table

You can also use union for single query.
select lt.name from location l join sales_person as lt on l.location_to = lt.id
UNION ALL
select lf.name from location l join sales_person as lf on l.location_from = lf.id

Related

Combining columns of the same name

So I have a two tables which somehow have the same column name. In the result query, i wish to combine the result which would then occupy the one that are empty.
Here is the structure of my tables:
position
accounts
employees
students
ssc
names
offices
This is my code:
SELECT app.approved, p.position, n.fname, n.mname, n.lname, o.office,
e.signature, ssc.signature
FROM approvals app
LEFT JOIN positions p ON p.position_id = app.position_id
LEFT JOIN accounts a ON a.account_id = app.account_id
LEFT JOIN employees e ON e.account_id = a.account_id
LEFT JOIN students s ON s.account_id = a.account_id
LEFT JOIN ssc ON ssc.students_id = s.students_id AND ssc.removed = 0
LEFT JOIN names n ON n.name_id = s.name_id OR n.name_id = e.name_id
LEFT JOIN offices o ON o.office_id = p.office_id
WHERE app.event_id = '10'
The actual result is this:
Notice that the some rows of e.signature are empty and the same for ssc.signature, so I wish to combine them, by the way, the SSC can never have its signature in the employees table.
Since you know the name of those columns that you wish to combine, you need to use the MySQL COALESCE() function to return the first non null value found in the list of provided arguments.
SELECT COALESCE(NULL, NULL, NULL, 'Aquaman', NULL, 'Some other dude');
This will return Aquaman.
In practice, you can use column names instead of raw values. For example:
SELECT app.approved, COALESCE(e.signature, ssc.signature) AS signature;
Note that the AS signature part is not necessary but it should that you can choose an alias for the combined field.
If the column name is the same then you can use the alias.
SELECT
p.column1 as p_column1,
p.product_name as p_product_name ,
s.product_name as s_product_name
FROM products p INNER JOIN suppliers s
For more info: http://www.mysqltutorial.org/mysql-alias/

INNER JOIN in mysql php

I have a little problem with INNER JOIN in mysql query. I have two tables the first is 'kontrola' and 2nd is 'naruszenia' In 'kontrola' table I have rows:
id
podmiot
miasto
wszczeto
zakonczono
naruszenie_id (Foreign KEY on 'naruszenia' table)
In my naruszenia table I have:
id
naruszenia
Now I want to display using INNER JOIN the naruszenie from 'naruszenia' table.
I've create somethin linke this:
$listakontroli = $connecting->query("SELECT * FROM kontrola INNER JOIN naruszenia ON
kontrola.naruszenie_id=naruszenia.id");
But the result is that when I want to display records in table I have changed the ID of first table(kontrola) and the naruszenia_id still showing id from naruszenia table. How to change it to display properly the word not id.
You could use explicit column name and refer to both the table (in this case using k an n) eg:
$listakontroli = $connecting->query("SELECT k.id
, k.podmiot
, k.miasto
, k.wszczeto7
, k.zakonczono
, n.naruszenia
FROM kontrola k
INNER JOIN naruszenia n ON k.naruszenie_id=n.id");
You need to use LEFT OUTER JOIN or separate the ID from the two tables. e.g.
$listakontroli = $connecting->query("SELECT kontrola.id as kid, naruszenia.id as nid, podmiot, miasto, etc* FROM kontrola INNER JOIN naruszenia ON kontrola.naruszenie_id=naruszenia.id");
This way you can properly distinguish the displayed IDs

PHP Join Query for Three Table When There is No Direct Common Column?

I have Three Tables in which first two tables have common column to match the record, and 2nd and third table also have common column but there is no direct matching column in first and third table. How shall I write join query ?
Table1(order) Column Names are order_id, patient_id, total, discount
Table2 (order_details) Column Names are order_details_id, order_id, test_id, result
Table3(tests) Column Names are test_id, test_name, test_normal_value
I hope it helps
SELECT * FROM `order`
LEFT JOIN `order_details` ON `order`.`order_id` = `order_details`.`order_id`
LEFT JOIN `tests` ON `order_details`.`test_id` = `test`.`test_id`
"SELECT a.patient_id FROM table1 as a
LEFT JOIN table2 as b on a.order_id = b.order_id
LEFT JOIN table3 as c on c.test_id = b.test_id";
to join this 3 table in one query, we need to assign each table a name. For example table1 AS NewNameTable1 LEFT JOIN table2 AS NewNameTable2. To connecting between this 3 table we need to have a foreign key for each table. So that this 3 table able to JOIN and we able to fetch the data in single query. As long this 3 table is JOINED via foreign key, you can echo out any attribute from any table that Joined.

MySQL SELECT statement with two WHERE clauses

Problem: How do I concatenate two MySQL tables using two different columns?
I have two MySQL Tables.
**DescriptionTable**. Fields: {filename, ID}.
**ResultsTable**. Fields: {query_id, media_id}. Both fields reference the ID field in the DescriptionTable.
A "match" links a query_id to a specified media_id, and an entry is added into ResultsTable.
I would like it so that I can do a SELECT query that retrieves the following:
[filename (query_id), filename (media_id)]
What I Have Tried:
SELECT a.filename
FROM DescriptionTable a, ResultsTable b
WHERE a.id = b.query_id
... but this only gives me the query_id's filename and not both of the filenames associated with query_id and media_id. How can I incorporate both in one SELECT command?
Join DescriptionTable to ResultsTable twice, once on query_id and once on media_id.
SELECT dq.filename as query_filename, dm.filename as media_filename
FROM ResultsTable r
INNER JOIN DescriptionTable dq ON r.query_id = dq.ID
INNER JOIN DescriptionTable dm on r.media_id = dm.ID
This should return rows with filenames for both query_id and media_id that correspond to the id pairs in the rows of ResultsTable.

MySQL Inner Join, selecting from multiple tables

I'm really struggling to get my head around this. I am trying to run a SELECT query from multiple tables.
This is what I have so far that doesn't work;
SELECT jira_issues.*, session_set.* FROM jira_issues, session_set
INNER JOIN reports on jira_issues.report_id = reports.id
WHERE jira_issues.report_id = 648
I have other tables (session_set, report_device) which has a ReportID and report_id column respectively.
I have a report table which has a Primary Key id. In the other tables the report.id key is linked with foreign keys.
Ultimately what I am trying to achieve is this:
I have an entry in the reports table with an id of 648. In the other tables (jira_issues, report_device, session_set), I also have entries which has a foreign key linked to the report id in the report table.
I want to run one SELECT Query to query the tables (jira_issues, report_device and session_set) and get all the data from them based on the report.id.
Thanks!
What about this:
SELECT * FROM jira_issues ji
LEFT JOIN session_set ss ON ji.report_id = ss.ReportID
LEFT JOIN report_device rd ON rd.report_id = ji.report_id
WHERE ji.report_id = 648;
Just say "no" to commas in the from clause. Always use explicit join syntax:
SELECT ji.*, session_set.*
FROM jira_issues ji inner join
reports r
on ji.report_id = r.id inner join
session_set ss
on ss.ReportId = r.report_id
WHERE ji.report_id = 648;
If some of the tables might have no corresponding rows, you might want left outer join instead of inner join.
Kindly try this out. You may get syntax error.
SELECT a., b. FROM jira_issues a, session_set b, reports c
Where a.report_id = c.id and b.report_id = c.id AND a.report_id = 648

Categories