Inserting specific fields into a mysql table - mysql

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

Related

MySQL LEFT JOIN is deleting the column of comparison on fail?

MySQL table schema: entity (key is entityId), post (key is entityId), photo (key is entityId)
Entity is going to be 1:1 to the combined post+photo tables, as these are basically subcategories of entity
sql request:
SELECT * FROM entity
LEFT JOIN post ON post.entityId = entity.entityId
LEFT JOIN photo ON photo.entityId = entity.entityId
This does return the full schema, but for entities that do not have a photo, the entityId returned is null. Every row that doesnt have a corresponding photo has its entityId set to null.
QUESTION: What is the cleanest way to combine these subgroups while maintaining all the data? I did manage to do it by doing only one, normal join with posts, and then union-ing that with another entity joined with photos, but my solution is very messy and theres probably a better way to do it.
You need to list out the columns explicitly. The problem you are having is the columns have the same name. For example:
SELECT e.*, pid as post_id, ph.id as photo_id
FROM entity e LEFT JOIN
post p
ON p.entityId = e.entityId LEFT JOIN
photo ph
ON ph.entityId = e.entityId;
If the only columns that have duplicate ids are the entityId columns, then you can use the USING clause (assuming your database supports it):
SELECT *
FROM entity e LEFT JOIN
post p
USING (entityId) LEFT JOIN
photo ph
USING (entityId)

Reading multiple fields on one table and joining them single line records from another table

I have two tables A and B. A has unique records while B may have several references to one record in A.
A table -> A.UserID,A.Image1.ID,A.Image2.ID,A.UserName
B table -> B.ImageID,B.ImageURL,ImageDescription
B. Image ID is unique and could have at least two records correspond to Image!ID and Image2ID in table A.
In my query, need to read A.UserName,B.Image1URL and B.Image2URL.
Following SQL query is to read one image. How I could modify this to read both Image1 and Image2 in one SQL query ?.
#"SELECT A.*,B.* FROM A
INNER JOIN B ON B.Image1ID = A.Image1ID
WHERE A.UserID = #Parameter1;";
So in the result, I need following :
UserID
Image1URL
Image2URL
What's the best way to get this done in mySQL ?
You can join same table twice -
SELECT A.UserID, A.UserName, img1.ImageURL, img2.ImageURL
FROM A
INNER JOIN B as img1
ON img1.ImageID = A.Image1ID
INNER JOIN B as img2
ON img2.ImageID = A.Image2ID
WHERE A.UserID = #Parameter1;

Doing a join on 3 tables

I have 3 sql tables
1) a table with headers of a coupon - id of this equals the id of the second table
2) a tables with details of the coupon - user_id on this tables equals user id of the third table
3) a table with details of user
So far I have this query
"SELECT kpn_processed_deals.kpn_id,
kpn_processed_deals.purchased_date, kpn_processed_deals.claim,
kpn_processed_deals.uid,kpn_deal_headers.kpn_type,
kpn_deal_headers.title,kpn_deal_headers.created_by
FROM kpn_processed_deals INNER JOIN kpn_deal_headers ON
kpn_processed_deals.kpn_id = kpn_deal_headers.kpn_id AND
kpn_deal_headers.created_by = '$var'";
This works just fine but I want to get the value of the users email on the third table using a join but I have been unsuccessful so far. Sorry if my formatting is messy. I'm horrible at these things.
Just add another JOIN.
"SELECT p.kpn_id, p.purchased_date, p.claim, p.uid,h.kpn_type, h.title, h.created_by, u.email
FROM kpn_processed_deals AS p INNER
JOIN kpn_deal_headers AS h ON p.kpn_id = h.kpn_id
JOIN kpn_deal_users AS u ON u.user_id = p.user_id
WHERE h.created_by = '$var'";
Also notice the use of table aliases, so you don't have to repeat the verbose table names throughout the query.
And constraints on single tables should normally be in the WHERE clause; the ON clause is for conditions related to joining the tables (an exception is in outer joins, where constraints on the child table need to be in the ON clause as well).

Updated rows not expected values with JOIN?

I've created several tables in a test database based on columns/rows in my main database.
test.websites.url = main.websites.url
test.category_main = main.websites.category1
test.category_01 = main.websites.category2
test.category_02 = main.websites.category3
etc...
The test database columns already contain all the rows from the main
database, but I need to add the rows from the respective tables to the
category_to_website table and create foreign keys because there is
currently no relation between them in the test database. That is why I have joined the
main database in the query.
When trying to use the main table as a reference for updating the existing rows in the test database, some values are updated but they are not always correct. I'm executing the query from the test database.
My query:
UPDATE category_to_website
LEFT JOIN main.websites
ON websites.url = main.websites.url
LEFT JOIN category_01
ON category_01.name = main.websites.category2
SET category_to_website.category_01_id = category_01.id
WHERE category_to_website.category_01_id = main.websites.category2
My database schema:
I suspect that the issue is with the type of JOINs I am doing, but I've tried LEFT JOIN, JOIN, and INNER JOIN and get the same results. I think that maybe I need a SELECT sub query or my WHERE clause is off?
EDIT
Based on the comments I was able to get this all sorted out. Here are the steps I took.
1. Merged the category_* tables into a category table.
2. Joined the test.websites table into the query.
UPDATE test.category_to_website
LEFT JOIN test.websites
ON test.websites.id = category_to_website.url_id
RIGHT JOIN main.websites
ON test.websites.url = main.websites.url
INNER JOIN test.category
ON test.category.name = main.websites.category1
SET category_to_website.category01_id = category.id
WHERE category_to_website.url_id = test.websites.id
UPDATE category_to_website
JOIN websites
ON websites.id = category_to_website.url_id
JOIN main.websites
ON websites.url = main.websites.url
JOIN category
ON category.name = main.websites.category1
SET category_to_website.category01_id = category.id
SETs are done to rows that participate in the JOINs. But if you LEFT JOIN to category_to_website then all its rows participate so then you must restrict them in a WHERE the way you already did in the ON.
Thank goodness you have started to relationalize that horrible schema. Keep going: replace all multiple categery_ columns in each table by just one category column for id or name. And if you named them category_id and category_name their nature would be clear. (Maybe post your next version as a new question.)

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

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;