SQL query on join table - mysql

I've the follow SQL schema:
+----------+
| products |
+----------+
| id |
| name |
+----------+
^ 8
|
v 1
+-------------+
| values |
+-------------+
| value |
| product_id |
| property_id |
+-------------+
^ 8
|
v 1
+------------+
| properties |
+------------+
| id |
| name |
+------------+
One product has many properties and a property belongs to many products. The values table is the join table for the many_to_many association between products and properties. And in this table is saved the value of the property for a product.
Now I'm looking for a query to select all products with property x with value a, and property y with value b ecc. My try is this query but return no records:
SELECT DISTINCT
products.*
FROM
products
INNER JOIN
product_values
ON product_values.product_id = products.id
INNER JOIN
properties
ON properties.id = product_values.property_id
WHERE
(properties.name = 'size' AND product_values.value = 'big')
AND (properties.name = 'color' AND product_values.value = 'red')
If possible I need a query with no nested select.

Since a property can not be color and size at the same time you need to use OR in your where clause. Then group the data and check if both are in the group with having
SELECT products.id, products.name
FROM `products`
INNER JOIN `product_values` ON `product_values`.`product_id` = `products`.`id`
INNER JOIN `properties` ON `properties`.`id` = `product_values`.`property_id`
WHERE (properties.name = 'size' AND product_values.value = 'big')
OR (properties.name = 'color' AND product_values.value = 'red')
GROUP BY products.id, products.name
HAVING count(distinct properties.name) = 2

I would do this using group by and having:
select pv.product_id
from product_values pv join
properties p
on pv.property_id = p.id
where (p.name, v.value) in ( ('size', 'big'), ('color', 'red') )
group by pv.product_id
having count(distinct p.name) = 2;

Another approach using sum to filter multiple attributes for an entity
SELECT
`p`.*
FROM
`products` p
INNER JOIN `product_values` v
ON `v`.`product_id` = `p`.`id`
INNER JOIN `properties` pr
ON `pr`.`id` = `v`.`property_id`
GROUP BY p.id
HAVING SUM (pr.name = 'size' AND v.value = 'big')
AND SUM(pr.name = 'color' AND v.value = 'red')

Related

Using parent id in child query?

SELECT li_1.carrier, li_1.product_id, li_1.quantity, products_description.products_name, sites.sites_id, sites.sites_name, counted_table.counted
FROM inventory li_1
INNER JOIN products_description ON li_1.product_id = products_description.products_id
INNER JOIN sites ON products_description.data = sites.data
INNER JOIN (
SELECT SUM( li_2.quantity ) AS counted
FROM inventory li_2
WHERE li_1.product_id = li_2.product_id
) counted_table
GROUP BY li_1.product_id
ORDER BY li_1.id DESC
I'm attempting to use the parent id (product_id) to count the total amount of quantity for each product in the subquery - But I only get the standard mysql error message.
So something like
id | quantity | total
---------------------------------
0001 | 2 | 6
| |
0001 | 4 | 6
What could be wrong?
if you change sub-query, it could be fixed
SELECT li_1.carrier, li_1.product_id, li_1.quantity, products_description.products_name, sites.sites_id, sites.sites_name, counted_table.counted
FROM inventory li_1
INNER JOIN products_description ON li_1.product_id = products_description.products_id
INNER JOIN sites ON products_description.data = sites.data
INNER JOIN (
SELECT product_id,SUM( li_2.quantity ) AS counted
FROM inventory li_2
GROUP BY li_2.product_id
) counted_table ON li_1.product_id = counted_table.product_id
ORDER BY li_1.id DESC

MySQL multiple inner join between 2 tables on different columns for one of the tables

Table transport
Id | FirstLevSubcat | SecondLevSubcat | ThirdLevSubcat
--------------------------------------------------------
8 | 4 | 27 | 1418
Table categories
Id | CategoriesUrl
--------------------
4 | cars
27 | audi
1418 | audi-100
Query if not to use categories table (without inner join) would be like
SELECT count(*) FROM transport
WHERE FirstLevSubcat = 4 AND SecondLevSubcat = 27 AND ThirdLevSubcat = 1418
Trying to get the same result using INNER JOIN
SELECT count(*) FROM transport main_table
INNER JOIN categories cat_table_first ON cat_table_first.IdRows = main_table.FirstLevSubcat
INNER JOIN categories cat_table_second ON cat_table_second.IdRows = main_table.SecondLevSubcat
INNER JOIN categories cat_table_third ON cat_table_third.IdRows = main_table.ThirdLevSubcat
WHERE
cat_table_first.CategoriesUrl = 'cars'
AND cat_table_second.CategoriesUrl = 'audi'
AND cat_table_third.CategoriesUrl = 'audi-100'
At first sight all works
But is such query ok? May be can improve something?
Your query is correct. You can also do it in following way:
SELECT count(*) FROM transport main_table
INNER JOIN categories cat_table_first ON cat_table_first.IdRows = main_table.FirstLevSubcat and cat_table_first.CategoriesUrl = 'cars'
INNER JOIN categories cat_table_second ON cat_table_second.IdRows = main_table.SecondLevSubcat and cat_table_second.CategoriesUrl = 'audi'
INNER JOIN categories cat_table_third ON cat_table_third.IdRows = main_table.ThirdLevSubcat and cat_table_third.CategoriesUrl = 'audi-100'
You can also do it using 3 EXISTS block.
SELECT count(*) FROM transport main_table
WHERE
EXISTS (SELECT NULL FROM categories WHERE main_table.FirstLevSubcat=categories.IdRows AND categories.CategoriesUrl ='cars')
AND
EXISTS (SELECT NULL FROM categories WHERE main_table.SecondLevSubcat=categories.IdRows AND categories.CategoriesUrl ='audi')
AND
EXISTS (SELECT NULL FROM categories WHERE main_table.ThirdLevSubcat=categories.IdRows AND categories.CategoriesUrl ='audi-100')

How can I select the users which are belonging to group A?

How can I select the users which are belonging to group A?
My tables are below.
my user table.
ID | name |sex
1 | bob |1
2 | kayo |2
3 | ken |1
my fos_group table
ID | name
1 | student
2 | teacher
my fos_user_user_group
user_id | group_id
1 | 1
2 | 2
3 | 1
Bob and Ken are belonging to group_1(student)
Kayo is belonging to group_2(teacher)
ex) I can select 'Bob' from user table like this
$query = $em->createQuery(
"SELECT p.name,p.sex
FROM UserBundle:User p WHERE
p.id = '1' );
But I would like to select the users which belongs to student group(Bob and Ken)
How should I change the sentence in createQuery?
I just guess I need to join the tables though...
additional....
I have tried like this accroding to Fabio's answer
$query = $em->createQuery(
"SELECT p,p.id,p.username,p.userKey
FROM UserBundle:User p
INNER JOIN fos_user_user_group b
ON a.ID = b.user_id
INNER JOIN fos_group c
ON b.group_id = c.ID
WHERE c.group_id = '1'");
$this->data["teachers"] = $query->getResult();
but it says
[Semantical Error] line 0, col 94 near 'fos_user_user_group': Error: Class 'fos_user_user_group' is not defined.
I guess it means I dont have entity for 'fos_user_user_group'.
I have only entity class for Group and User,other tables were created automatically.
In meanwhile,I used like this in other place in $formmapper.
->add('teacher',
null,
array(
'query_builder' =>
function (\Doctrine\ORM\EntityRepository $rep) {
return $rep->
createQueryBuilder('s')
->join('s.groups', 'g')
->where('g.name = :group')->setParameter('group','TeacherGroup');
})
)
it works well,
how can I change this sentence for createQuery()?
I think you can use a INNER JOIN query
SELECT p.name,p.sex
FROM User p
INNER JOIN fos_user_user_group b
ON a.ID = b.user_id
INNER JOIN fos_group c
ON b.group_id = c.ID
WHERE c.group_id = '1'

Can I fire two select statements in a single MySQL command and count resulting rows?

Here's my code as is, working, but very slow:
$graded = R::getAll("SELECT posts.id, posts.discussion, rating.rating, rating.itemid
FROM uv_forum_posts posts
JOIN uv_rating rating ON ( posts.id = rating.itemid )
WHERE posts.discussion = :discussion_id
GROUP BY posts.userid",
array(':discussion_id' => $discussion['id']));
$total = R::getAll("SELECT posts.userid
FROM uv_forum_posts posts
WHERE posts.discussion = :discussion_id
GROUP BY userid",
array(':discussion_id' => $discussion['id']));
$percentages[] = count($graded) / count($total) * 100;
$graded represents all rows that have a rating.
$total represents all user participation, regardless of being graded or not.
I'm only interested in the numerical values of the resulting sets, can I combine these two MySQL calls into a single call that returns two numbers graded and total?
If I correctly understand your requirements you can try
SELECT p.userid,
COUNT(*) total,
COUNT(r.itemid) graded
FROM uv_forum_posts p LEFT JOIN
uv_rating r ON p.id = r.itemid
WHERE p.discussion = :discussion_id
GROUP BY p.userid
Output:
| USERID | TOTAL | GRADED |
---------------------------
| 1 | 8 | 2 |
| 2 | 4 | 4 |
SQLFiddle
UPDATE: If you just want grand total for all posts and users then
SELECT COUNT(*) total,
COUNT(r.itemid) graded
FROM uv_forum_posts p LEFT JOIN
uv_rating r ON p.id = r.itemid
WHERE p.discussion = 1
Output:
| TOTAL | GRADED |
------------------
| 12 | 6 |
SQLFiddle
You could simply use a query as in the format :
Select
( SELECT posts.id, posts.discussion, rating.rating, rating.itemid
FROM uv_forum_posts posts
JOIN uv_rating rating ON ( posts.id = rating.itemid )
WHERE posts.discussion = :discussion_id
GROUP BY posts.userid,
array(':discussion_id' => $discussion['id']))) gradeCount,
(SELECT posts.userid
FROM uv_forum_posts posts
WHERE posts.discussion = :discussion_id
GROUP BY userid,
array(':discussion_id' => $discussion['id'])))totalCount

conditions (where) mysql

Table products:
id|name
-------
1 |computer
2 |microwave
3 |transl
Table product_features:
feature | id_product | feature_value
------------------------------------
count_of_buttons | 1 | 1
count_of_buttons | 2 | 2
count_of_buttons | 3 | 1
color | 1 | white
color | 2 | white
color | 3 | black
Pls, how to get all white products with one button?
Thank you very much!
select product.*
from product
join product_features as buttons
on buttons.id_product = product.id
join product_features as color
on color.id_product = product.id
where buttons.feature_value = '1'
and buttons.feature = 'count_of_buttons'
and color.feature_value = 'white'
and color.feature = 'color';
select
p.id
p.name
from
products p
join (select * from product_features where feature = 'color') colors on (p.id=colors.id_product)
join (select * from product_features where feature = 'count_of_buttons') buttons on (p.id=buttons.id_product)
where
colors.feature_value = 'white'
and buttons.feature_value = 1
You might consider reorganizing the product_features table so that you have a separate column for each feature (i.e. a color column and a count_of_buttons column) so that you have one row for each product. In fact it could all be in the products table.
SELECT
p.id
FROM products p
JOIN product_features pf
ON p.id = pf.id
WHERE pf.feature_value = 'white' AND pf.count_of_buttons = 1
select p.id, p.name from products p inner join product_features ON p.id=id_product where feature_value='white' and feature='color' and count_of_buttons=1