Select query to select from two tables - mysql

I have two table in the PostgreSQL DB which looks like
Select Productid,productname,quantity,availabilty from public.product
And other table is
Select sampleid,samplename,Productid from public.sample
So here I need to select the Products and their sample from the product table and sample table where Productid in the Product table is equal to the productid in the sample table. Should I be using the Joins here.I am confused with the Left and right join

I need to select the Products and their sample
I think you can use LEFT JOIN here to get all products and matching samples if any:
select p.*,
s.sampleid,
s.samplename
from public.product p
left join public.sample s on s.Productid = p.Productid;

Related

How to select multiple fields in MySQL query when using INNER JOIN

I want to select multiple fields from table p.
The second line of this code is wrong. how to write?
except p.*
I don't want p.*
SELECT
p.id, title, price
c.`title` as `CategoryTitle`
from `tbl_products` p
INNER JOIN `tbl_categories` c
ON p.`category_FK` = c.`id`
Well, you might have your own reason so, perhaps you can do something like this:
SELECT
id, title, price, CategoryTitle
FROM `tbl_products` p
INNER JOIN
(SELECT `title` AS 'CategoryTitle', id AS 'CategoryID'
FROM `tbl_categories`) c
ON category_FK = CategoryID
Make one of the table as a subquery and define column alias that's not a duplicate with the other table. In your example, it seems like both of your tables have columns with similar names like id & title. Once you define those similar column names in the subquery with different alias, then you won't need to do p.xx or c.xx.

Keeping Records That Are Not Joined MYSQL

I have the following query in my application:
SELECT
p.old_product_id,
l.product_id,
p.sku,
p.title,
p.option_one,
p.option_two,
FROM
lookup_id l
JOIN temp_price_tables_data p USING (sku);
And it works great. However, a small percentage of records from the temp_price_tables_data tables don't make it to the results.
This is because the skus from the lookup_id table don't exist in the temp_price_tables_data.
Is there a way to keep these records in the new data?
Or is there a way to only get those records so I can store the result for later processing?
EDIT:
First table columns = old_product_id, sku, title, option_one, option_two
Second table column = product_id, sku
Tables should have SKU in common.
Use a left outer join:
SELECT
*
FROM
lookup_id l
LEFT OUTER JOIN price_tables_data p on l.sku = p.sku
WHERE old_product_id IS NULL;
That will get you all the records that are in temp_price_tables_data but not in lookup_id

SQL average and Join

I'm trying to merge these two statements into one query to get the a list of product names(or ids) against the average of their TTFF data, and I'm stuck.
select AVG(TTFF) from TTFFdata group by product_id
select product.product_name, count(*) from product join TTFFdata on product.product_id = TTFFdata.product_id
I've looked into using a temporary table (CREATE TEMPORARY TABLE IF NOT EXISTS averages AS (select AVG(TTFF) from TTFFdata group by product_id)) but couldn't get that to work with a join.
Anyone able to help me please?
You need to understand the components. Your second query is missing a group by. This would seem to be what you want:
select p.product_name, count(t.product_id), avg(t.TTFF)
from product p left join
TTFFdata t
on p.product_id = t.product_id
group by p.product_name
It is better to do group by on product_id, product_name for two reasons. One is, you can select product id along with product name. Second reason is, If the product name is not unique then it may give wrong results(this may be a rare scenario like product name is same but it differs based on other columns like version or model). The below is the final query.
select Product.product_id,
product_name,
AVG(TTFF) as Avg_TTFF
from Product
inner join
TTFFdata
on Product.product_id = TTFFdata.product_id
group by Product.product_id,Product.product_name
TTFFdata:
product:
Output:

How to join a MYSQL fulltext search with another table?

Hello thanks for reading this:
I have a Product Table in which i am carrying out a full text search on but need to combine it with a product image table
SELECT * FROM product WHERE Match(productBrand,productTags,ProductDiscription,productCondition,productColour,productCat) Against('top');
I need to join by product ID.
My productImage table contains a productID and a productURL;
You can use query something like below-
SELECT prd.*,pim.productURL FROM product AS prd
JOIN productImage pim ON prd.productID=pim.productID
WHERE MATCH(prd.productBrand, prd.productTags, prd.ProductDiscription, prd.productCondition, prd.productColour, prd.productCat) AGAINST('top');
Use of Limit:
SELECT prd.*,pim.productURL FROM product AS prd
JOIN productImage pim ON prd.productID=pim.productID
WHERE MATCH(prd.productBrand, prd.productTags, prd.ProductDiscription, prd.productCondition, prd.productColour, prd.productCat) AGAINST('top')
LIMIT 1;

How to create view in query having JOINS?

Hello I am not able to create view for following query.
create view my_View as
select * from product p
LEFT JOIN product_description pd ON (p.product_id=pd.product_id)
I have two tables
TABLE1 :-
Product with column as :- product_id, column2,column3
TABLE2 :- Product_Description with column as:- product_id , column4,column5.
Here product_id is foreign key for Product table.
Following is the error I am getting
Error Code: 1060. Duplicate column name 'product_id'
I am using mysql workbench to run my queries. Looking at the error, I understand that I need to assign alias. But I am required to join more than 5-8 tables thats the reason I am looking for better solution.
The Error is coming due to same column name in both the tables, second thing is its never a good practice to fetch all records in views by using * until and unless you really want all records from all table that you are using in join condition.
What you can do is select records specific to your view, like
select p.*,pd.somefield from product p
LEFT JOIN product_description pd ON (p.product_id=pd.product_id)
In the above query your product_id is fetched from product table only and you can fetch only necessary fields from product_description table
The easiest solution in your case is to use using instead of on:
create view my_View as
select *
from product p LEFT JOIN
product_description pd
USING (product_id);
In general, though, you should list the columns explicitly in the select rather than using *.
SELECT * will select all columns from both tables in the join. But both tables have a column called product_id, so you'll get two columns in the view with the same name.
You need to list out all the columns, so you can give an alias to product_description.product_id to distinguish it:
SELECT p.product_id, p.column2, p.column3, pd.product_id AS pd_id, pd.column4, pd.column5
FROM product AS p
LEFT JOIN product_description AS pd ON p.product_id = pd.product_id