Combine inner join result to one column - mysql

I have one shop table and one item table. Shop table has many item, and my problem is I want to search multiple items that available in one shop.
shop table
id | name
---------
1 | Shop 1
2 | Shop 2
Item table
id | name | shop_id
----------------------
1 | JRC | 1
2 | sukhoy | 1
3 | sukhoy | 2
When I want to find item jrc and sukhoy, so it must showing Shop 1, because both two items are ready on Shop 1.
My expected output is
Output table
id | shopName | itemName
------------------------
1 | Shop 1 | JRC
2 | Shop 1 | sukhoy
My query is
select * from shops
inner join products as produk2 on produk2.shopId = shops.id and (produk2.name like "%sukhoy%")
inner join products as produk on produk.shopId = shops.id and (produk.name like "%jrc%")
It works because it using different alias per inner join.
But what I want is, how to combine the output from that 2 join without define different alias. Or how I can combine join result into one same column ?

I think this does what you want:
select p.shop_id
from products p
group by p.shop_id
having sum(p.name like '%jrc%') > 0 and
sum(p.name like '%sukhoy%') > 0;
This returns shop ids that have both products. Of course, you can join the results to shops to get more information about the shops.

You can just use one join:
select * from shops
inner join products as produk
on produk.shopId = shops.id
and (produk.name like "%jrc%" OR produk.name like "%sukhoy%")

Related

MySQL double group

I am making queries to extract data from database which holds customer order. There's one table which holds customer id's and the customer's name. Another table which has the order id, customer id of who placed the order, a quantity of the item bought, and an item id. The last table holds the item id's and item names. I am trying to sort these to show an individual's most popular purchase, but am having issues properly grouping and ordering to produce the correct result, below is an example of what is intended.
customers
1 | John
---+-----
2 | Jane
orders
1 | 2 | 4 | 1
---+---+---+---
2 | 2 | 5 | 2
---+---+---+---
3 | 2 | 2 | 1
---+---+---+---
4 | 1 | 1 | 2
items
1 | Chair
---+-------
2 | Sofa
After properly sorting and grouping, the output table should like:
John | Sofa
------+------
Jane | Chair
Currently I can connect the item names to the purchaser and return a random item bought, but not the most popular by quantity. I have tried entering multiple fields into group by and managed to properly group the items by name and sort by quantity, but in doing so the customer id's became ungrouped. Been trying to solve this for days so any help would be appreciated. Please note that this is a very simplified version of the actual problem where many more tables are involved, including multiple items table which are being joined together to one.
You should use group by on joined tables
select
b.name
, c.name
, sum(quantity) as tot
from orders as a
inner join Customers as b on a.customer_id = b.id
inner join Items as c on a.item_id = c.id
group by b.name, c.name
order by tot
Selecting the sum of the quantities per customer-item group is easy, but selecting the top seller is a bit harder.
The first step is the query to get all the groups with the sums of the quantities for each customer-item:
SELECT
customer_name,item_name,SUM(quantity)
FROM
orders o
JOIN customers c ON o.customer_id=c.id
JOIN items i ON o.item_id=i.id
GROUP BY customer_name,item_name;
Then to only select the groups with the maximum quantity sums we use some trickery:
SELECT
customer_name,item_name,SUM(quantity),
(SELECT SUM(quantity) AS qmax
FROM
orders o2
JOIN customers c2 ON o2.customer_id=c2.id
JOIN items i2 ON o2.item_id=i2.id
WHERE c2.id=c.id
GROUP BY c2.customer_name,i2.item_name
ORDER BY qmax DESC LIMIT 1) AS qmax
FROM
orders o
JOIN customers c ON o.customer_id=c.id
JOIN items i ON o.item_id=i.id
GROUP BY customer_name,item_name
HAVING SUM(quantity)=qmax;
Edit:
Here's a link to a fiddle: SQLFiddle

consult sql join tables

I try make this consult in sql, but i dont know how.
I have a tables of product shops.
And i want a table similar this:
tshirt | jeans | number sells
---------------------------------------
tshirt1 | jean1 | 5
tshirt1 | jean2 | 4
tshirt1 | jean3 | 0
.............................
tshirt1 | jeanN | 3
This information i have in other table where i have shopping.
This table have a code of users and a row for buy with one product.
codeUser product buy
-------------------------
1 tshirt1
1 jeans1
2 jeans2
...............
I want know many people buy a tshirt1 and also buy one product of category2.
I have a view with product filter by category, example tshirt; and other view equals but filter other category, example jeans.
I need this view??, or i need other view??
I dont know how I will do the query
Thank you
First, you need to generate all the rows using a cross join. Then bring in the customer information.
I think it looks like this:
select t.product as tshirt, j.product as jeans,
count(distinct bj.codeUser)
from (select distinct product from buys where product like 'tshirt%') t cross join
(select distinct product from buys where product like 'jeans%') j left join
buys bt
on bt.product = t.product left join
buys bj
on bj.product = j.product and
bj.codeuser = bt.codeuser
group by t.product as tshirt, j.product;

getting count of each category filtered by another table's field

I have tables with the following structure.
AD_TABLE -
ID|NAME|CAT_ID|TYPE
1| car | C0101|Sale
2|bike | C0201|Want
CAT_TABLE -
ID |NAME |PARENT|LEVEL
C0100|Vehicle |C0100 | 0
C0101|Car |C0100 | 1
C0200|Bike/Scooters |C0100 | 1
C0201|Bike |C0200 | 2
C0202|Scooter |C0200 | 2
I need to get the count of ADs from each category, I have written the following query.
SELECT `CAT_TABLE`.`ID`,`CAT_TABLE`.`NAME`,`CAT_TABLE`.`LEVEL`,`CAT_TABLE`.`PARENT`, COUNT(`AD_TABLE`.`ID`)
FROM `CAT_TABLE`
LEFT JOIN `AD_TABLE` ON `AD_TABLE`.`CAT_ID`=`CAT_TABLE`.`ID`
WHERE (`CAT_TABLE`.`ID`='C0100' OR `CAT_TABLE`.`PARENT`='C0100') AND `AD_TABLE`.`TYPE`='0'
GROUP BY `CAT_TABLE`.`ID`
I got the count of each categories properly but after including the AD_TABLE.TYPE`='0' in the where clause categories which do not have ADs were ignored. I need to get all the categories even if the count is 0.
try this
SELECT `CAT_TABLE`.`ID`,`CAT_TABLE`.`NAME`,`CAT_TABLE`.`LEVEL`,`CAT_TABLE`.`PARENT`, COUNT(`AD_TABLE`.`ID`)
FROM `CAT_TABLE`
LEFT JOIN `AD_TABLE`
ON `AD_TABLE`.`CAT_ID`=`CAT_TABLE`.`ID`
AND `AD_TABLE`.`TYPE`='0' -- Write and here..<br/>
WHERE (`CAT_TABLE`.`ID`='C0100' OR `CAT_TABLE`.`PARENT`='C0100')
GROUP BY `CAT_TABLE`.`ID`

MySQL Left Join Limit on subquery selecting min(value)

Sorry if the title is not very clear...
I have two tables products and images,
related on products.product_id = images.product_id,
each product has several images related to it on the images table,
both tables have a column width values I wish to ORDER BY (product_order and image_order).
The result I'm looking for is a list with all the published products with only one image (and one result) per product, the image should be the one with the lowest value on the column image_order.
I put together the code below from other answers I found here and it's working, the only problem is that the column image_order allows duplicates, so where there are images for the same product with the same value in image_order I'm getting multiple results per product.
SELECT products.*, images.image_name, images.image_order
FROM products
LEFT JOIN images ON (
images.product_id = products.product_id
AND images.image_order = (
SELECT min(image_order)
FROM images
WHERE product_id = products.product_id
)
)
WHERE products.published = 1
ORDER BY products.product_order ASC
edit (My tables):
Table products
product_id | product_name | product_order
____________|________________|_________________
1 | Fist Product | 1
2 | Second Product | 2
3 | Third Product | 3
Table images
image_id | product_id | image_name | image_order
____________|________________|__________________|_________________
1 | 1 | Fist Image URL | 1
2 | 1 | Second Image URL | 1
3 | 2 | Third Image URL | 1
4 | 3 | Fourth Image URL | 1
The results I'm getting from this Query:
Firt Product + First Image
Firt Product + Second Image
Second Product + ThirdImage
Third Product + FourthImage
The results I need:
Firt Product + First Image
Second Product + ThirdImage
Third Product + FourthImage
You can use something like this, but it's not the best option. With further knowlodge of your tables a better answer might be possible:
select products.*,
(
select image_name from images
where product_id = products.product_id
order by image_order asc
limit 1
) as 'image_name'
from products
WHERE products.published = 1
ORDER BY products.product_order ASC

Specific MySQL issue with JOIN

I have a product table:
product_id
shop_id -> id from shop table
product_pair = there is product_id, if it is paired
Then I have a shop table:
shop_id
And finally a shipping table:
shop_id -> id from shop table
country_id -> id of country
And I want to find the products which can be shipped to country_id 60
It's no problem, if it's not paired..
Like:
SELECT p.*, c.*, p.product_name AS score
FROM (`rcp_products` p)
JOIN `rcp_shipping` s ON `s`.`shop_id` = `p`.`shop_id` AND s.country_id = 60
JOIN `rcp_category` c ON `c`.`cat_id` = `p`.`cat_id`
WHERE `p`.`cat_id` = '7'
AND `p`.`product_price_eur` > 0
AND `p`.`product_mark_delete` = 0
ORDER BY `score` asc
LIMIT 10
(There are some additional WHERE's and another columns, which I think haven't got influence)
Now, I have paired products. So, in a table with products is something like this:
product_id | product_name | product_pair | shop_id
1 | Abc | 0 | 0
2 | Def | 1 | 3
3 | Ghi | 1 | 2
So, products 2 and 3 are paired to product 1.
Now, I have no idea how to get country_id for product_id = 1 in that SQL that I posted above.
Maybe my database structure is not the best :) But how can I do it better?
Thank you.
Overall, the idea that you need to use here is self-join - that's how you can find the pairs of products. After that it's just simple WHERE conditions.
The core query (the one that just finds the pairs from a specific shop) would look like this:
SELECT DISTINCT A.product_id as P1, B.product_id as P2, A.shop_id as S1, B.shop_id as S2
FROM products A, products B
WHERE (A.product_pair = B.product_id OR A.product_pair = 0) //find pair and non-paired
AND (A.product_id > B.product_id) //ensure no duplicates (e.g. A&B and B&A)
AND (A.shop_id = B.shop_id) //ensure that both can be found in the same shop
AND A.shop_id = YOUR_SHOP_ID //filter to specific shop
This should satisfy the conditions when products are sold in more than 1 shop, otherwise the query could probably become a bit shorter / easier.