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.
Related
So I have the following chart here where I have columns:
a b c d
1 1 1 3
1 1 1 4
1 1 1 5
2 2 2 1
2 2 2 3
2 2 2 3
3 3 3 4
3 3 3 5
3 3 3 6
What I want to do is add and average column d where columns a,b,c contain the same values. How would I go about doing this?
I imagine it would be something like
Select SUM(Table.d) where a = b AND b = c AS e
Try this:
Select SUM(Table.d), AVG(Table.d) from Table Group by Table.a, Table.b, Table.c
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%'
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 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.
I have these tables.
tb_employee:
ID_EMP NAME_EMP
1 Employee 1
2 Employee 2
3 Employee 3
4 Employee 4
tb_requirements:
ID_REQ DESCRIPTION_REQ TYPE_REQ
1 Requirement 1 1
2 Requirement 2 1
3 Requirement 3 1
4 Requirement 4 2
5 Requirement 5 2
6 Requirement 6 2
7 Requirement 7 2
tb_detail:
ID_DET ID_EMP ID_REQ
1 1 1
2 1 2
3 1 4
4 2 1
5 2 6
6 3 4
7 3 7
I need to make a SELECT QUERY to count how many requirements each employee has got and which type, like this:
ID_EMP NAME_EMP TYPE_REQ1(virtual column) TYPE_REQ2 (virt. c.)
1 Employee 1 2 4
2 Employee 2 1 1
3 Employee 3 0 2
4 Employee 4 0 0
I really don't know how to do it.
Try this one
SELECT
e.ID_EMP
,e.NAME_EMP
,(CASE WHEN SUM(r.TYPE_REQ=1) IS NULL THEN 0 ELSE SUM(r.TYPE_REQ=1) END ) TYPE_REQ1
,(CASE WHEN SUM(r.TYPE_REQ=2) IS NULL THEN 0 ELSE SUM(r.TYPE_REQ=2) END ) TYPE_REQ2
FROM
tb_employee e
LEFT JOIN tb_detail d ON (e.ID_EMP=d.ID_EMP)
LEFT JOIN tb_requirements r ON (d.ID_REQ=r.ID_REQ)
GROUP BY e.ID_EMP