INNER JOIN in mysql php - 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

Related

JOIN shows data twice in mysql?

SELECT *
FROM `all_salary_slip_details` `assd`
INNER JOIN `employee_master` `em` ON `assd`.`EMPLOYEE_ID` = `em`.`EMPLOYEE_ID`
INNER JOIN `employee_details` `ed` ON `assd`.`EMPLOYEE_ID` = `ed`.`EMPLOYEE_ID`
INNER JOIN `organizations` `o` ON `o`.`ORG_ID` = `assd`.`ORG_ID`
INNER JOIN `months` `mo` ON `mo`.`id` = `assd`.`PAY_MONTH`
This is my query to fetch data from db. I have only 4 rows but in my view I get 48 rows. I dont understand why this happens.
You need to use a GROUP BY clause on a column, then your query will only return a single row for each unique entry in that column.
In your case I'm guessing it would make sense to use the employee name.
What result does this query output?
SELECT assd.PAY_YEAR, assd.PDF, em.EMPLOYEE_NAME, o.ORG_NAME, mo.month_name
FROM all_salary_slip_details assd
INNER JOIN employee_master em
ON assd.EMPLOYEE_ID = em.EMPLOYEE_ID
INNER JOIN organizations o
ON o.ORG_ID = assd.ORG_ID
INNER JOIN months mo
ON mo.id = assd.PAY_MONTH
GROUP BY em.EMPLOYEE_NAME
You will get 1 row returned in your query for each record in a joined table that matches the join condition.
So, if you have multiple records in any one of the employee_master, employee_details, organisations or months tables that match those in all_salary_slip_details you will get additional rows returned. Look at the rows returned, you will see differences in each and where they differ will tell you where the join condition for the table isn't specific enough to return a single row.

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.

How to join 4 tables in mysql for getting different colums data on same html table?

Image of what I want from 4 tables:
As You can see there are
1.com_name
2.emp_type_name
3.loc_name
3 different tables with their primary keys: com_id,emp_type_id and loc_id.
Now, there is master table called organization_tbl and it is having org_id. org_id is FK in all the tables.
I want to display com_name from company_tbl of org_id=1 emp_type_name from emp_type table where org_id=1 and loc_name from location_tbl where org_id=1 in where clause if I put organization_tbl.org_id=1 then this should be the output have to come but i got below output duplicate values.
SELECT emp_type.emp_type_name,location_tbl.loc_name,company_tbl.com_name
from organization_tbl
left outer join company_tbl
on company_tbl.org_id=organization_tbl.org_id
left outer join location_tbl
on location_tbl.org_id=organization_tbl.org_id
left outer join emp_type
on emp_type.org_id=organization_tbl.org_id
GROUP BY emp_type.emp_type_name,location_tbl.loc_name,company_tbl.com_name,
where organization_tbl.org_id=1
This is my query that not working as I want.
It just looks like your WHERE is in the wrong place to me:
SELECT
employer.emp_type_name,
location.loc_name,
company.com_name
FROM organization_tbl organization
LEFT JOIN company_tbl company
on company.org_id=organization.org_id
LEFT JOIN location_tbl location
on location.org_id=organization.org_id
LEFT JOIN emp_type employer
on employer.org_id=organization.org_id
WHERE organization.org_id = 1
GROUP BY
employer.emp_type_name,
location.loc_name,
company.com_name

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

Mysql return result if no result in table 2

Let's say I has this sql line:
SELECT (something)
FROM user_data, user_profile_image
WHERE user_data.user_id='6' (AND user_profile_image.user_id='6')
How do I make sure that it returns informations from the user_data field if there is no containing data in user_profile_image related to that user?
You need to use JOIN
SELECT u.*,i.user_profile_image FROM user_data u LEFT JOIN `USER_PROFILE_IMAGE_TABLE` i ON i.user_id=u.user_id WHERE u.user_id='6'
I am unsure what your USER_PROFILE_IMAGE_TABLE is so you will need to change that.
SELECT something
FROM user_data
LEFT JOIN user_profile_image
ON user_profile_image.user_id = user_data.user_id
WHERE user_data.user_id = '6'
AND user_profile_image.user_id IS NULL;
You want to LEFT JOIN the detail page on the foreign key field being equal to the primary key field of the first table, and select the rows from the first table such that there IS no matching row from the second table.
In the query you gave, there's no join criteria, so it would be a Cartesian product - every row of user_data merged with every single row of user_profile_image.
Try this:
SELECT u.user_id, u.user_name, IFNULL(up.user_id, 'No Data In user_profile_image') UserId,
IFNULL(up.user_image_id, 'No Data In user_profile_image') user_image_id
FROM user_data u
LEFT JOIN user_profile_image up ON u.user_id = up.user_id
WHERE u.user_id = 6;
Check this link SQL FIDDLE DEMO

Categories