How can I retrieve data from 3 tables in mysql - join assistance? - mysql

Table layout:
Hi, I would like to retrieve a combination of information from the above tables, primarily a single row of results from the 'episode' table, but also some information from the 'tvshow' table and the 'season' table, that also only applies to that single row I want to retrieve from the 'episode' table, this is my query at the moment:
Select tvshow.series_name, season.season_banner, season.season_poster, episode.season_num, episode.episode_num, episode.episode_name, episode.plot
FROM tvshow,season,episode
WHERE episode.tvshow_id = 1 AND episode.season_num = 1 and episode.episode_num = 1
I'm aware I'd need to use a join, but i'm unsure how to go about doing this, with regards to combining the results from 3 tables.
The closest i've got to returning a single row is this:
SELECT tvshow.series_name, season.season_banner, season.season_poster, episode.season_num, episode.episode_num, episode.episode_name, episode.plot
FROM episode
INNER JOIN tvshow ON episode.tvshow_id = tvshow.tvshow_id
INNER JOIN season ON episode.season_num = season.season_num
WHERE episode.tvshow_id =1
AND episode.season_num =1
AND episode.episode_num =1
That returns the row I want, yet also returns an identical second row aside from the fields that only exist in the season table (season banner & poster) being blank.

First you need to normalize your data base.
Your table structure like thata
tvshow --> tv_id as primary key
season --> tv_id as foreign key
episode --> tv_id as foreign key
Your join query
Select tvshow.series_name, season.season_banner, season.season_poster, episode.season_num, episode.episode_num, episode.episode_name, episode.plot
FROM tvshow
JOIN season on season.tv_id= tvshow.tv_id
JOIN episode on episode.tv_id= tvshow.tv_id
WHERE episode.tv_id = 1

Use Left Join
select ts.series_name,ss.season_banner,es.season_num from tvshow as ts inner join season as ss on ss.tvshow_id=ts.tvshow_id inner join episod as es on ss.season_num=es.season_num where ts.tvshow_id=1 and ss.season_num=2 and es.episode_num=1;

Related

Inserting specific fields into a mysql table

I have 4 tables in a mysql db. A joining table, Author table, Output Table, and a Person table. I had to import a csv file that had an outputID and personID but only have access to the author names column, so i've added an auto increment primary key to the author table.
I want the query I've made to insert the author Id's into the joining table where the specific author related to the ID matches the specific outputID.
The following query keeps inserting the author Id's but all at the bottom of the the joining table, without any matching output id
INSERT INTO output(a_fk)
SELECT a.Author_ID
FROM authors a
INNER JOIN wholecsv w ON a.Author_Names = w.a_Author
INNER JOIN outputlist ot ON ot.Output_ID = w.a_ID
INNER JOIN output o ON o.Output_ID = ot.Output_ID
The following query keeps inserting the author Id's but all at the bottom of the the joining table, without any matching output id
That's what INSERT does : adding new records to the table.
It looks like you are actually looking for an UPDATE query. For this, you need to adjust the JOIN logic so it goes from output to authors, like :
UPDATE output o
INNER JOIN outputlist ot ON o.Output_ID = ot.Output_ID
INNER JOIN wholecsv ON ot.Output_ID = w.a_ID
INNER JOIN authors ON a.Author_Names = w.a_Author
SET o.a_fk = a.Author_ID

Left Join when looking for no matches

I have 3 tables and I am pretty sure I need to use a left join when joining the 3rd however the join between the first 2 tables I think just needs to be a regular join and I'm not sure if that requires some kind of nesting or not.
So the first 2 tables (once I set my conditions) should always work out to a 1 to 1 relationship. Then I need to join that to the 3rd table but I need to know if there is no match (which means i need a left join here). Essentially that's all I need to know in this query and actually want to filter table 3 and only show if it is NULL. Furthermore those NULL responses I want to see (or lack thereof) have a date field. I also want to only see that there is NO record for today's date in table 3.
At the end of the day I want to know when there is no record existing in table 3 for todays date for the primary key in table 1/2 (since the way that first join works even though there is a primary/foreign key relation, the primary key of table 1 is all that matters when matching on table 3.
Query:
SELECT
subscribers.*,
check_times.*
FROM subscribers, check_times
LEFT JOIN checks
ON checks.subscriber_id = check_times.subscriber_id
WHERE
subscribers.subscriber_id = check_times.subscriber_id
AND check_times.dow = 1
AND check_times.time <= '19:52'
AND checks.date = '2015-02-16'
AND checks.status IS NULL
Since you're referencing the table in the LEFT JOIN on the WHERE clause, it became an INNER JOIN. What you want is to put them in the JOIN condition.
SELECT
subscribers.*,
check_times.*
FROM subscribers,
INNER JOIN check_times
ON subscribers.subscriber_id = check_times.subscriber_id
LEFT JOIN checks
ON checks.subscriber_id = check_times.subscriber_id
AND checks.date = '2015-02-16'
WHERE
check_times.dow = 1
AND check_times.time <= '19:52'
AND checks.status IS NULL
Also, please refrain from using the old-style JOIN. Read on this for more info:
Bad habits to kick : using old-style JOINs

Query multiple tables including rows with null foreign keys MySQL

It's my first time with mysql and I need some help. I couldn't find any solution to this probably because it's too easy or I'm too noob to look it up properly yet.
I want to make a query from multiple tables from my test database. Let's say tables are:
category (*id_category, *id_product)
product (*id_product, id_category, id_supplier, id_manufacturer)
manufacturer (*id_manufacturer, name)
supplier (*id_supplier, name)
The columns with * are integer primary key not null with auto_increment(starting from 1). The other id columns are just integer foreign keys(default null).
I need a query also including rows with null id_manufacturer and/or id_supplier on the product table. Those empty fields in the product table return value '0', so when I make a query like
SELECT category.id_product, manufacturer.name, supplier.name
FROM category, product, manufacturer, supplier
WHERE category.id_category = product.id_category AND manufacturer.id_manufacturer = product.id_manufacturer AND supplier.id_supplier = product.id_supplier;
None of the rows with null values(0)are shown.
I'd like you to point me in the most clean and efficient way to do it without changing table properties. Any help would be very much appreciated.
You need to use Left Join then.
SELECT category.id_product, manufacturer.name, supplier.name, P.ID_PRODUCT
FROM product
LEFT JOIN category C
ON C.id_category = P.id_category
LEFT JOIN manufacturer M
ON M.id_manufacturer = P.id_manufacturer
LEFT JOIN supplier S
ON S.id_supplier = P.id_supplier;
These will give all the rows of product table even if it has no corresponding row from other tables.

MySQL merge 2 tables with same Id values

I have a MySQL db that has 2 tables that have related information that I need to merge to 1 table.
Gallery has an itemid that relates to rbitems Id. Both tables gallery and rbitems have different column names but both have unique data. I want to merge the two tables based on the Id and itemid columns.
So how do I merge 2 different tables into 1 based on 1 column having unique values. I'd like to just append the other tables to the merge.
You can use JOIN to solve your problem.
SELECT a.*, b.*
FROM galley a INNER JOIN rbitems b
on a.itemid = b.id
CREATE TABLE new_table
AS (SELECT g.itemid, g.a, g.b, g.c, r.x, r.y, r.z
FROM gallery g INNER JOIN rbitems r
ON g.itemid = r.id
);
You may wish to add "AS name" to the members of the select clause.
If you have elements of the original tables that are not represented in the other table you should look into RIGHT, LEFT, or OUTER JOIN instead of INNER.
This assumes that the itemid and id columns are unique--a given itemid/id does not exist multiple times in the same table.

MySQL Join with many (fields) to one (secondary table) relationship

I have a query I need to perform on a table that is roughly 1M records. I am trying to reduce the churn, but unfortunately there is a UNION involved (after i figure this join out), so that may be a question for another day.
The records and data I need to get reference 3 fields in a table that need each pull a description from another table and return it in the same record, but when i do the Inner join i was thinking, it either returns only 1 field fromt he other table, or multiple records from he original table.
Here are some screen shots of the tables and their relationship:
Primary table containing records (1 each) with the physician record I want to pull, including up to 3 codes that can be listed in the "taxonomy" table.
Secondary table containing records (1 each) with the "Practice" field I want to pull.
A Quick glance of the relationship i'm talking about
I presume that if perform an inner join matching the 3 fields in the physicians table, that it will have to iterate that table multiple times to pull each taxonomy code .. but I still can't even figure the syntax to easily pull all of these codes instead of just 1 of them.
i've tried this:
SELECT
taxonomy_codes.specialization,
physicians.provider_last_name,
physicians.provider_first_name,
physicians.provider_dba_name,
physicians.legal_biz_name,
physicians.biz_practice_city
FROM
taxonomy_codes
INNER JOIN physicians ON physicians.provider_taxonomy_code_1 = taxonomy_codes.taxonomy_codes OR physicians.provider_taxonomy_code_2 = taxonomy_codes.taxonomy_codes OR physicians.provider_taxonomy_code_3 = taxonomy_codes.taxonomy_codes
First, the query churns a lot and it only returns one taxonomy specialty result which I presume is because of the OR in the join statement. Any help would be greatly appreciated.
Thank you,
Silver Tiger
You have to join the taxonomy_codes table multiple times:
SELECT p.provider_last_name, p...., t1.specialization as specialization1, t2.specialization as specialization2, t3.specialization as specialization3
FROM physicians p
LEFT JOIN taxonomy_codes t1 ON t1.taxonomy_codes = provider_taxonomy_code_1
LEFT JOIN taxonomy_codes t2 ON t2.taxonomy_codes = provider_taxonomy_code_2
LEFT JOIN taxonomy_codes t3 ON t3.taxonomy_codes = provider_taxonomy_code_3