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;
Related
I am trying to learn more about EAV and I was doing some testing on a magento 2 sample data database.
I just want to retrieve the the product id and the description of the product but there is a mismatch with the total amount of products in the catalog_product_entity table and the catalog_product_entity_text table:
There are 2046 products in the catalog_product_entity table:
If I use the following query I get 2052 results:
SELECT product.entity_id as "Description",description.value FROM catalog_product_entity_text description,catalog_product_entity product where product.entity_id = description.entity_id ORDER BY product.entity_id
I assume there are a few cases where an entity_id matches more than one row in the text table. Perhaps there can be a text attribute other than description?
Try this:
SELECT product.entity_id as Product_id,
COUNT(*) AS count,
GROUP_CONCAT(description.value) AS Description
FROM catalog_product_entity_text description
LEFT OUTER JOIN catalog_product_entity product ON product.entity_id = description.entity_id
GROUP BY product.entity_id
ORDER BY product.entity_id
I am not familiar with Magento's EAV tables, but I assume the table should have a column for the attribute identifier as well as an entity_id. You may have to filter based on the attribute type, if you just want the Description, and not other text attributes.
P.S.: I adapted your query to use modern JOIN syntax. You shouldn't use the "comma-style" joins, they went out of style in 1992.
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;
I am trying to write a mysql query for an app I'm developing for android.
I have a database that has a bill_content table and a products table
I want to select top 10 most sold products.
This is a minimal version of what I have, but it's all I need to get an answer here.
bill_content table has the columns: id, id_product, quantity (id_product and quantity here can be duplicate because this table is larger, containing id_bill and other information)
products table has the columns: id, name
SELECT products.name AS Product,
bill_content.quantity AS Quantity
FROM bill_content, products
WHERE bill_content.id = products.id
ORDER BY bill_content.quantity DESC
LIMIT 10
Of course this returns a table of 2 rows containing all the products and their quantity in the bill_content table, but there are duplicates and I need to make sum of their quantity and display them as a single row.
Thank you in advance.
ANSWERED
This could be done using GROUP BY as Gordon Linoff said.
You want a group by. You should also learn to use proper explicit join syntax:
SELECT p.name AS Product,
SUM(bc.quantity) AS Quantity
FROM bill_content bc JOIN
products p
ON bc.id = p.id
GROUP BY p.name
ORDER BY SUM(bc.quantity) DESC
LIMIT 10;
table: users
columns: id
table: products
columns: id
table: product_images
columns: product_id | path
table: cart
columns: user_id | product_id
product_id in the bottom 2 tables corresponds to id in the products table.
user_id in the cart table corresponds to id in the users table.
My friend and I are developing a generic shopping website just for fun.
The requirement: Given a user_id, go through the user's shopping cart and return all corresponding rows in the products table and with exactly 1 image for the product (there may be multiple images).
This is our query (which seems to work):
SELECT products.*, product_images.path
FROM products
INNER JOIN cart
ON cart.product_id = products.id AND cart.user_id = 13 /* arbitrary id */
LEFT OUTER JOIN product_images
ON product_images.product_id = cart.product_id
GROUP BY cart.product_id
The first join is intuitive to me because both tables involved in the join are joining on columns within each table.
It is the 2nd join that I am confused about.
My understanding is that the first join will produce a virtual table.
This virtual table is then joined with the product_images table, but the confusing part is that the ON condition is not using a column belonging directly to the virtual table.
So what exactly is going on here?
Please note that I know how to rewrite the query in a way that is more intuitive to me, and that it is understanding the concept that is important to me.
At first I thought this was uncommon, but I noticed w3schools does the same thing at the bottom of this link: http://www.w3schools.com/sql/sql_groupby.asp
cart and products are inner joined.
The virtual table going on to the next join includes all columns from cart and all columns from products.
This virtual/logical table will be as below.
SELECT products.id,
cart.user_id,
cart.product_id
FROM products
INNER JOIN cart
ON cart.product_id = products.id
AND cart.user_id = 13;
So there are three columns in this virtual table. products.id,cart.user_id,cart.product_id
This virtual table is then left joined onto product_images using a column from product_images and cart.product_id from the virtual table.
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