Pulling data from two tables (for a 'related products' query) in MySQL - mysql

I'm trying to pull data from two tables for a 'related products' widget.
I've tried all the JOINS and UNIONS I can and still get nothing.
The first table (productdocs) stores documents. The second (prodrelated) shows when a product is related to a document:
productdocs
pdid (unique ID for the document)
pdname (name of the uploaded document)
prodrelated
prprodid (the ID for the PRODUCT)
pritemid (the ID for the document)
I am trying to output the productdocs.pdname for any documents that match up with the product's id. In otherwords, show the pdname when:
WHERE productdocs.pdid = prodrelated.pritemid
I would post my SQL code, but none of it has worked, so I think it would be pointless. I hope I explained this correctly given my frazzled brain - Any help greatly appreciated.

You can use a simple INNER JOIN for this, e.g.:
SELECT pd.pdid, pd.pdname
FROM productdocs pd JOIN prodrelated pr ON pd.pdid = pr.pritemid
WHERE pd.prprodid = <any_id>;
If you don't want to filter out any records, you can get rid of WHERE clause and it will output all the records.
Here's MySQL's documentation for JOIN.

Wow you guys are fast - thank you so much.
Darshan - thank you above all, I was able to make a few mods to what you wrote and it worked great. I tried to +1 your answer but maybe I don't have enough 'reputation'? Here is what I got working, thanks to you:
SELECT pd.pdid, pd.pdname
FROM productdocs pd
JOIN prodrelated pr
ON pd.pdid = pr.pritemid
WHERE pr.prprodid = '#url.prodid#'
In the future I will try to post some code example, but on this one I honestly tried at least 7 different queries so I had no idea which to post!

Related

SQL Is this Outer or Inner Join and how to join on Array

Seemed simple when I started and have done this before, now I confused myself and at a road block.
Have two tables: News_Table and a People_Table. Under the News_Table there is a field: News_People_Contributed and it has the ID's of the People_Table in array format (1,4,7,10) thus Four People contributed. I am creating a search parameter that looks up News_Header AND News_People_Contributed and can't figure how to create the search column.
News_Table
News_ID
News_Header
News_People_Contributed
People_Table
People_ID
People_First_Name...
Is it something like...
Select*
From News_Table
Left Join News_Table
On People_Table.People_ID IN (News_Table.News_People_Contributed)
Where Search_Param Like '%News_Header%' OR Search_Param Like '%People_First_Name%'
The problem is (News_Table.News_People_Contributed) is a string and the ID's are not. Plus I may not have people contributed etc. To make the issue even more complex, I'm doing this in MS Access instead of MySql, so have to code it "old school" sql for work around.
Perform a cross join and filter on matches in the string list. It says nothing about efficiency or form (as already commented on), but it works.
SELECT *
FROM News_Table, People_Table
WHERE InStr([News_People_Contributed],CStr([People_ID])) > 0;
This only answers part of the problem: The join -- the issue everyone seemed concerned about in the initial comments. There are not enough details about about the Search_Parameter to provide help on that. Supply more detail if you need more help there.

Select all elements from a table, and check if they match to another table

I have two tables, and I want to take all entities from the first table, then, check if they can be related to a specific entity from another table. If they can be related the database returns 1 (or true), else it returns NULL (or false).
I tried some things with LEFT JOIN but none of them work. I think the solution is simple but I can't figure it out...
Context : In my application I make two requests, the first one take all entities from oneTable, the second one take all idOT from anotherTable where idAT is equal to 2, THEN, I make a loop where I save all entities from the first request and in this loop I make another loop where I check if the current element is present on the second request. I thought they this solution was to heavy (two requests and imbricated loops) so I tried to do it directly in one request.
Thank you for your help guys ! I hope it won't make you lose your time...
Edit : #Strawberry gave me the answer in the comments, I was doing
SELECT * FROM oneTable LEFT JOIN anotherTable ON oneTable.idOT = anotherTable.idOT **WHERE** anotherTable.idAT = 2
instead of
SELECT * FROM oneTable LEFT JOIN anotherTable ON oneTable.idOT = anotherTable.idOT **AND** anotherTable.idAT = 2
It's as simple as that... Thank you again guys.
Thank to the comments of the question the correct request is :
SELECT * FROM oneTable
LEFT JOIN anotherTable ON oneTable.idOT = anotherTable.idOT
AND anotherTable.idAT = 2;

Append string to another table in MySQL

I’ve got a problem selecting some things in my database.
I’ve got 1 database with a lot of tables, however I only use 2 with this specific task.
I have a Clothing table, and I need to import this in my new database. I have succesfully transferred a lot of data, but now I need to take 1 final small step into completing this. In my Clothing table I have a column called Youtube link, here I have a link with an Youtube link, this specific link, in this specific column, I want to append that to another table I have in the database, let’s call it new_clothing_table. I there have a description column called prod_desc and I want to append the Youtube link to that column.
But there is also another “problem”, it’s not that every product has a Youtube link, so things have to be filtered in order to not fuck things royally up. A advantage I have, I have in both tables a product_name, and these are all the same. So I want to transfer that specific Youtube link if it’s there (sometimes there is a 0 filled in or nothing, but I dont think it’s NULL because if I make a SELECT query where Youtube_link is null I get zero rows..)
So can someone help me out>?
mysql has an update-join construct you can use:
UPDATE new_clothing_table nct
JOIN clothing c ON c.product_name = nct.product_name AND
c.youtube_link != '0'
SET nct.description = CONCAT(nct.description, c.youtube_link)
EDIT:
To make sure this does what you want, you could first select the updated content in order to examine it:
SELECT nct.description AS old_description,
CONCAT(nct.description, c.youtube_link) AS new_description
FROM new_clothing_table nct
JOIN clothing c ON c.product_name = nct.product_name AND
c.youtube_link != '0'

inner query of subqery returning multiple rows

I am not that experience in sql so please forgive if its not a good question to ask,but i researched around almost for 3-4 days but no able to solve.
My problem is i have a table which have multiple image names in it,so what i have to do is whoever is the follower of a particular user i have to get the imaged from this table,so one user there can be multiple followers,so i have to fetch the images posted by all the followers.
Here is the subquery code snippet i am using.
SELECT id,
outfit_image,
img_title,
description
FROM outfitpic_list r2
WHERE Email=ANY(SELECT being_followed
FROM follower_table
WHERE follower='test#gmail.com')
So the inner query here returns multiple values,for each value(being_followed) i have to fetch all the images and display it,but with this query each time i get only one image.I tried IN also but didnot work out.
Table structure:-
Outfitpic_list table
id|outfit_image|datetime|Email|image_title|description
Follower_table
bring_followed|follower
Please help,I am stuck..!!
Thank you..!!
I think your problem may be the = sign between "E-mail" and "Any". Try this statement:
SELECT
id,
outfit_image,
img_title,
description
FROM outfitpic_list r2
WHERE Email IN
(
SELECT being_followed
FROM follower_table
WHERE follower='test#gmail.com'
)
It's the same statement, without the = sign, and the ANY keyword replaced with IN. (I cleaned it up a little to make it more readable)

MYSQL Selecting multiple values from the same column as a row

I have two tables
table COMMUNITY
id,
name,
etc.
table PROPERTY
id,
community_id,
type,
price,
rate,
etc.
There are four possible property types and a community can have info for one or more of the different types.
I am trying to figure out how to combine the information for each community into one row.
For instance I might want to get a row which includes
community.id, community.name, condo.price, condo.rate, town_home.price, town_home.rate
Of course there aren't tables for condo or town_home, the property type is represented in the property table by a column and a community can have multiple property rows.
My first thought was to use multiple joins on the table with aliases, but I can't seem to get anything that works properly.
Any suggestions?
You can use left join for this.
SELECT c.id, c.name, condo.price, condo.rate, town_home.price, town_home.rate
FROM Community c
LEFT JOIN Property condo ON condo.community_id = c.id AND condo.type = 'condo'
LEFT JOIN Property town_home ON town_home.community_id = c.id AND town_home.type = 'town_home'
I am the kind of person who doesn't like much to use joins, might be because I don't see much reason as for use them or as I wouldn't need it.
By any means, this is the way I would put it:
As you have a link between the two tables, you are able to use them to bring you the data you need, as long as you have this link you can bring whatever field you want.
So you said you wanted the fields:
community.id, community.name, condo.price, condo.rate, town_home.price, town_home.rate
SELECT
community.id,
community.name,
condo.price,
condo.rate
FROM
community, property condo
WHERE
community.id = condo.community_id;
This should solve it.
Try this:
SELECT * FROM community LEFT OUTER JOIN property ON community.id = property.community_id;
This should join the two tables on the ID field, so you should have one complete row with all of the information. if anything is null, it should be present in the row, as well. If multiple properties are listed with the same community ID, they should all come up in the result.
PS - I use PostgreSQL and not MYSQL so the syntax might be a bit different. Let me know how it works!