MySQL Inner Join, selecting from multiple tables - php

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

Related

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

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.

How Do I JOIN a third Table called all_recipes using recipe_id KEY

SELECT COU.recipe_id, IFNULL(FOU.Found, 0) AS Found, COU.Count
FROM ( SELECT recipe_id, COUNT(recipe_id) As Count
FROM recipe_ingredients
GROUP BY recipe_id) COU
LEFT OUTER JOIN ( SELECT R.recipe_id, COUNT(R.key_ingredient) AS Found
FROM users_ingredients U
JOIN recipe_ingredients R ON R.key_ingredient = U.key_ingredient
GROUP BY R.recipe_id) FOU ON FOU.recipe_id = COU.recipe_id
I need to Join three tables together - well, 4 in the end. Can someone please show me how I can do this and explain how to keep joining when required later. I will have 4 or more tables. Many Thanks
Credit to #Arulkumar for Above
I don't really understand what your SQL does but, to join another table you need to do something like this.
SELECT recipe.id, all_recipes.id
FROM recipe
LEFT JOIN all_recipes ON recipe.id = all_recipes.id

Search keyword and apply filters

I am encountering a problem I cannot bypass by myself and I that's why I am posting this question. There are a lot of other posts out there that give me half of the answer and I don't really know how to get it done.
I have 3 tables that contain informations about an ad. One table is "ad_names", another is "ad_locations" and the last one is "ad_details".
Ad_Names has : ad_title, ad_description, ad_date_added
Ad_Locations has : ad_country, ad_region, ad_city
Ad_Details has : ad_price, ad_author, ad_active
Basically I want apply location and details filters for an ad with a certain title.
For example, I want to search "food" keyword in ad-titles and then apply filters like "only from Kansas" or "Only from Kansas + price higher than 500USD". How do I do it?
first you will need to join the tables. do you know if you have a foreign key on the tables? like is there an Ad_ID field on all 3 tables? if the tables are large you may also want to index the fields you are searching. once you have all the data you will "filter" it with a where clause.
http://www.w3schools.com/sql/sql_where.asp
select * from ad_names as A
join ad_location as L on A.key=L.key
join ad_details as D on A.key=D.key
where A.ad_title like '%food%' and L.ad_region = 'Kansas' and D.ad_prics > 500;
depending on how your database is setup, you may get a cartesian result there, so you would have to look at your join. maybe a left outer join limiting the join in an on statement instead of in the where. it is hard to say without more information.
Join types
Assuming that Ad is a table with primary key id, which is a foreign key in each of the other tables (ad_id) then something along the lines of:
Location:
SELECT *
FROM Ad A
INNER JOIN Ad_Names N ON N.ad_id = A.id
INNER JOIN Ad_Locations L ON L.ad_id = A.id
INNER JOIN Ad_Details D ON D.ad_id = A.id
WHERE N.ad_title LIKE '%food%' AND L.ad_city = 'Kansas'
Price and location:
SELECT *
FROM Ad A
INNER JOIN Ad_Names N ON N.ad_id = A.id
INNER JOIN Ad_Locations L ON L.ad_id = A.id
INNER JOIN Ad_Details D ON D.ad_id = A.id
WHERE N.ad_title LIKE '%food%' AND L.ad_city = 'Kansas' AND D.ad_price > 500

MySql find data in one table based on conditions of two other tables

Help me out with this query:
I have 3 tables with this structure.
items_to_groups (item_id | group_id)
item_to_regions (item_id | region_id)
items [a bunch of columns]
I need to select every row on the item table that has an item_id match on item_to_groups table WHERE group = x AND has an item_id match on item_to_regions table WHERE region = y
Currently the code I have is a horrible subquery with loops and all.
What would be a better way of doing this?
I've thought about JOIN and such, but can't really get my head around on how to do it.
SELECT bunch_of_columns
FROM items i
INNER JOIN items_to_groups ig ON i.id=ig.item_id
INNER JOIN items_to_regions ir on i.id=ir.item_d
WHERE ir.region_id=y
AND ig.group_id=x
Have a look at the JOIN documentation on MySQL. Joins are important for relational databases.
As you said you have a hard time grasping joins, have a look at A Visual Explanation of SQL Joins by Jeff Atwood. Maybe it helps.
SELECT colums
FROM items
INNER JOIN items_to_groups ON items.item_id = items_to_groups.item_id AND group_id = x
INNER JOIN items_to_regions ON items.item_id = items_to_regions.item_id AND region_id = y
SELECT * FROM items
JOIN items_to_groups ON (items.item_id = items_to_groups.item_id AND group_id = ?)
JOIN items_to_regions ON (items.item_id = items_to_regions.item_id AND region_id = ?)
GROUP BY items.item_id

Categories