MySQL get data with table joins - mysql

I have two tables. One is ps_product_lang and ps_category_product. The data inside ps_category_product is like this
id_category id_product
2 1
2 2
2 3
2 4
2 5
2 6
2 7
3 1
3 2
3 3
3 4
3 5
3 6
3 7
4 1
4 2
5 1
7 2
8 3
8 4
8 5
8 6
8 7
9 3
10 4
The table for ps_product_lang is like this
id_product id_lang name
1 1 Faded Short Sleeves T-shirt
1 2 Faded Short Sleeves T-shirt
2 1 Blouse
2 2 Blouse
3 1 Printed Dress
3 2 Printed Dress
4 1 Printed Dress
4 2 Printed Dress
5 1 Printed Summer Dress
5 2 Printed Summer Dress
6 1 Printed Summer Dress
6 2 Printed Summer Dress
7 1 Printed Chiffon Dress
7 2 Printed Chiffon Dress
So here I want to get the id_product,name from ps_product_lang where id_category is 5,7 and name like 'p%'. so can someone tell me what would be the query. Any help and suggestions would be really appreceable.

As #Gordon was hinting, you simply need an INNER JOIN between the two tables, along with a where condition to restrict the product category and name:
SELECT t1.id_product,
t1.name
FROM ps_product_lang t1
INNER JOIN
(
SELECT DISTINCT id_product
FROM ps_category_product
WHERE id_category IN (5, 7)
) t2
ON t1.id_product = t2.id_product
WHERE t1.name LIKE 'P%'
Here is a demo which omits the WHERE clause to show how the query behaves when it actually returns data:
SQLFiddle

Use JOIN to get data from 2 tables
SELECT p.id_product,name
FROM ps_product_lang p
JOIN ps_category_product c
ON p.id_product=c.id_product
WHERE id_category IN (5,7)
AND name LIKE 'p%'

Related

how to display result with order by based parent and childs in one table

I got problem with how to display query result based by parent and child on one table
data for example:
ID
CATEGORY
PARENT
ROOT
1
FOOD & DRINK
0
NULL
2
CLOTHES
0
NULL
3
FRUIT
1
1
4
DESERT
1
1
5
PANTS
2
2
6
T-SHIRT
2
2
7
APPLE
3
1
8
BANANA
3
1
9
ICE CREAM
4
1
10
SHORTS
5
2
11
LONG T-SHIRT
6
2
and I want to sort and display to this:
ID
CATEGORY
PARENT
ROOT
2
CLOTHES
0
NULL
5
PANTS
2
2
10
SHORTS
5
2
6
T-SHIRT
2
2
11
LONG T-SHIRT
6
2
1
FOOD & DRINK
0
NULL
4
DESERT
1
1
9
ICE CREAM
4
1
3
FRUIT
1
1
7
APPLE
3
1
8
BANANA
3
1
is there any query script to display like above ?
here my code and have no luck
select
id, category, parent, root
from
tbl_menu
order by
coalesce(root,parent,id),
category
and thanks for help :)
I understand that you want to order the tree nodes depth-first. One option uses a recursive query to generate the path to each node, that you can then use for ordering:
with recursive cte as (
select t.*, cast(id as char) as path from mytable t where root is null
union all
select t.*, concat(c.path, '/', t.id)
from cte c
inner join mytable t on t.parent = c.id
)
select *
from cte
order by path

Select record from table with not in other table

I am stuck in query I have a table like this
budget_details
id budget_id expenditure_head_id budget
1 1 1 1233
2 1 2 333
3 1 3 567
4 2 1 343
5 2 2 343
6 2 3 6767
7 2 4 557
expenditure_heads
id name
1 abc
2 xyz
3 qwe
4 uvw
I want to get all the expenditure_heads from budget_details that even
if not in budget_details like here budget_id=1 does not contain expenditure_head_id 4
but I want to select that to with 0 or null displaying
I tried this but it not displaying expenditure_head_id 4
select `expenditure_heads`.`name`, `budget_details`.`budget`, `budget_details`.`id` from
`budget_details`
right join `expenditure_heads` on `budget_details`.`expenditure_head_id` = `expenditure_heads`.`id`
where `budget_details`.`budget_id` = 1"
The where avoid you to get the missing row you need. The left join is done on the ON statement, so this query should work for you:
SELECT EH.name, BD.budget, BD.id FROM expenditure_heads EH
LEFT JOIN budget_details BD
ON (BD.expenditure_head_id = EH.id AND BD.budget_id = 1)

MySQL Query results AND

What I have is 3 tables: fruits, categories and fruit_category
fruits:
fruit_id | fruit_name
1 apple
2 orange
3 mango
4 banana
5 lanzones
6 pineapple
fruit_category:
fruit_id | cat_id
1 1
2 9
3 7
4 7
5 8
6 9
1 9
2 7
3 9
4 1
5 3
6 5
categories
cat_id | cat_name
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
When I execute a query looking for fruits that are either in categories 7 or 9 I get the correct results
fruit_name
apple
orange
mango
banana
pineapple
But when I use the AND operator, I always get 0 results. Below is my query for AND (same for OR)
SELECT `o`.`fruit_name`
FROM `fruits` AS `o`
JOIN `fruit_category` AS `oc`
ON `o`.`fruit_id` = `oc`.`fruit_id`
JOIN `categories` AS `c`
ON `c`.`cat_id` = `oc`.`cat_id`
WHERE `oc`.`cat_id` = 7
AND `oc`.`cat_id` = 9
You should use logical or (one value can't be equal to two unequal values at the same time), represented by in here:
where `oc`.`cat_id` in ( 7, 9)
Which is equivalent to:
where `oc`.`cat_id` = 7 or `oc`.`cat_id` = 9
If you want to ensure, that fruit_name is in both categories, you should use group by and having clauses after the where (that is represented above, with or):
group by `o`.`fruit_name`
having count(distinct `oc`.`cat_id`) = 2
SELECT
o.fruit_name
FROM
fruits as o
JOIN
fruit_category as oc
ON
o.fruit_id = oc.fruit_id
JOIN
categories as c
ON
c.cat_id = oc.cat_id
WHERE
oc.cat_id IN (7,9)
GROUP BY
o.fruit_name
HAVING
COUNT(oc.cat_id) = 2

How to get total cost of product in mysql from following sql tables?

How Do i Calculate total cost of product from the following tables?
Table 1: components
pk_comp_id comp_name fk_comp_type_id com_price
1 Vertical Glass Panels 3 50.00
2 Side Frame 2 32.00
3 Front Frame 2 35.00
4 Roof Section 3 50.00
5 Door Frame 1 100.00
6 Standard Timber Plank 2 100.00
7 Brackets 6 20.00
8 Door Section 2 50.00
Table 2: products
pk_prod_id prod_name fk_prod_type_id
1 Small Green House 1
2 Large Green House 1
3 Small Shed 2
4 Small Summer House 3
5 Large Summer House 3
And Table 3: products_components
fk_prod_id fk_comp_id number
1 1 20
1 2 2
1 3 2
1 4 2
1 5 1
2 1 40
2 2 4
2 3 2
2 4 4
2 5 1
3 6 35
3 7 60
3 8 1
HERE is what i did so far
SELECT
SUM(`components`.`com_price`*`products_components`.`number`) AS com_g_total,
`products`.`prod_name`, `products`.`pk_prod_id`
FROM `products`
INNER JOIN `products_components` ON `products_components`.`fk_prod_id` = `products`.`pk_prod_id`
INNER JOIN `components` ON `components`.`pk_comp_id` = `products_components`.`fk_comp_id`
But this gives only one row where as i was suppose to get three row with total cost of each for pk_prod_id 1,2,3.
Add a GROUP BY clause:
SELECT
SUM(`components`.`com_price`*`products_components`.`number`) AS com_g_total,
`products`.`prod_name`, `products`.`pk_prod_id`
FROM `products`
INNER JOIN `products_components` ON `products_components`.`fk_prod_id` = `products`.`pk_prod_id`
INNER JOIN `components` ON `components`.`pk_comp_id` = `products_components`.`fk_comp_id`
GROUP BY `products`.`pk_prod_id`
Result:
COM_G_TOTAL PROD_NAME PK_PROD_ID
1334 Small Green House 1
2498 Large Green House 2
4750 Small Shed 3
See result in SQL Fiddle.

Getting unique values as a query result

I have a many-to-many relation with my database, so I created a sql query which later I will use for search.
Now what I want to do is get a unique values from my search result (unique company names).
This is my query:
SELECT agencies.company
,agencies.website_url
,agencies.STATUS
,agencies.size
,IndustryData.industry_id
,ProfessionData.profession_id
,SectorData.sector_id
,seniorityData.seniority_id
,ZonesData.zone_id
FROM agencies
LEFT JOIN (
SELECT agencies_industries.agency_id
,agencies_industries.industry_id
FROM agencies_industries
) AS IndustryData ON agencies.id = IndustryData.agency_id
LEFT JOIN (
SELECT agencies_professions.agency_id
,agencies_professions.profession_id
FROM agencies_professions
) AS ProfessionData ON agencies.id = ProfessionData.agency_id
LEFT JOIN (
SELECT agencies_sectors.agency_id
,agencies_sectors.sector_id
FROM agencies_sectors
) AS SectorData ON agencies.id = SectorData.agency_id
LEFT JOIN (
SELECT agencies_seniorities.agency_id
,agencies_seniorities.seniority_id
FROM agencies_seniorities
) AS SeniorityData ON agencies.id = SeniorityData.agency_id
LEFT JOIN (
SELECT agencies_zones.agency_id
,agencies_zones.zone_id
FROM agencies_zones
) AS ZonesData ON agencies.id = ZonesData.agency_id
WHERE IndustryData.industry_id = 3
AND ProfessionData.profession_id = 1
The result looks like this:
company, website_url, status, size, industry_id, profession_id, sector_id, seniority_id, zone_id
Nine nine.com 1 3 3 1 3 2 1
Nine nine.com 1 3 3 1 3 2 5
Nine nine.com 1 3 3 1 3 2 8
Nine nine.com 1 3 3 1 3 5 1
Nine nine.com 1 3 3 1 3 5 5
Nine nine.com 1 3 3 1 3 5 8
Ten ten.com 2 3 3 1 3 1 1
Ten ten.com 2 3 3 1 3 1 3
Ten ten.com 2 3 3 1 3 1 7
Ten ten.com 2 3 3 1 3 3 1
Ten ten.com 2 3 3 1 3 3 3
Ten ten.com 2 3 3 1 3 3 7
Ten ten.com 2 3 3 1 3 5 1
Ten ten.com 2 3 3 1 3 5 3
Ten ten.com 2 3 3 1 3 5 7
I would like to get rid of the repeats of the company names. How do I do that?
You could use a GROUP BY for this, e.g.:
SELECT agencies.company
,agencies.website_url
,agencies.STATUS
,agencies.size
,IndustryData.industry_id
,ProfessionData.profession_id
,SectorData.sector_id
FROM ...
GROUP BY agencies.company
,agencies.website_url
,agencies.STATUS
,agencies.size
,IndustryData.industry_id
,ProfessionData.profession_id
,SectorData.sector_id
Maybe you could do that with variables and subqueries, as some examples shown here:
http://dev.mysql.com/doc/refman/5.0/en/user-variables.html
But maybe doing this at your code is faster than in your query.