Mysql: group by two column from separated tables - mysql

I have posts and products. Posts is related with Products.
I need count all posts updated at current month (2017-07), but related product update date also is important.
Post3 is also updated this month, because have a product Product3.
If i try GROUP BY Posts.date_update, Products.date_update, but get incorect number.
Posts
id | Name | date_update
1 | Post1 | 2017-07
2 | Post2 | 2017-08
3 | Post3 | 2017-08
Products
id | Name | date_update
1 | Product1 | 2017-07
2 | Product2 | 2017-08
3 | Product3 | 2017-07
4 | Product4 | 2017-09
PostsToProducts
id | post_id | product_id
1 | 1 | 2
2 | 1 | 3
3 | 1 | 4
4 | 2 | 4
5 | 3 | 2
6 | 3 | 3

Assuming you always have at least one Product related to an existing Post you could do something like :
SELECT COUNT(DISTINCT(Posts.Name)) FROM Posts, Products, PostsToProducts WHERE
Posts.id = PostsToProducts.post_id
AND
Products.id = PostToProducts.product_id
AND
(Posts.date_update = '2017-07' OR Products.date_update = '2017-07');

Related

Count nested select using join and by month

Hi I have three tables here is all
First. count_internets
id| company_id | item_id | created_at
1 | 1 | 1 | 2020-10-14 |
2 | 1 | 2 | 2020-10-15 |
3 | 2 | 4 | 2020-10-16 |
4 | 2 | 5 | 2020-11-20 |
5 | 1 | 1 | 2020-11-22 |
6 | 1 | 1 | 2020-11-23 |
7 | 2 | 5 | 2020-11-23 |
Second compaies
id | name
1 | Company 1
2 | Company 2
Third items
id | name | company_id
1 | Product 1 | 1
2 | Product 2 | 1
3 | Product 4 | 2
4 | Product 5 | 5
I want to get infomartion how many items sold during one month grouping by company like below
| Company | Products | Month | Count
| Company 1 | Product 1 | OCT | 1
| Company 1 | Product 2 | OCT | 1
| Company 1 | Product 1 | NOV | 1
| Company 1 | Product 4 | OCT | 1
| Company 2 | Product 5 | NOV | 2
I tried many SQL queries, but I can not solve. Please help to solve this query.
This one should work
SELECT c.name as company, i.name as products, EXTRACT(MONTH FROM FROM_UNIXTIME(created_at)) as month, count(*)
FROM count_internets
JOIN companies as c on company_id = c.id
JOIN items as i on item_id = i.id
GROUP BY company, products, month
Be aware that if that the query extract only the month (so no difference between years). If you need the year too you have to extract it EXTRACT(YEAR FROM FROM_UNIXTIME(created_at)) as year.
But your example is not very clear, company 1 didn't sell product 3. Furthermore, what's the meaning of the company_id in items table?

Update records based on date on MySQL using joins

I have all those tables above.
car_model_tbl
-----------------------------
id | car_model_name|status |
-----------------------------
1 | seria_1 | 1 |
-----------------------------
2 | golf_4 | 1 |
-----------------------------
3 | C_Class | 1 |
-----------------------------
4 | golf_5 | 1 |
-----------------------------
5 | seria_2 | 0 |
-----------------------------
car_manufacturer_tbl
-------------------------
id |car_manufactu_name |
-------------------------
1 | bmw |
-------------------------
2 | volkswagen |
-------------------------
3 | mercedes |
-------------------------
car_service_tbl
---------------------------------
id | model_id| service_date |
---------------------------------
1 | 1 | 2018-03-10 |
---------------------------------
2 | 2 | 2018-02-10 |
---------------------------------
3 | 1 | 2018-01-10 |
---------------------------------
4 | 1 | 2017-12-10 |
---------------------------------
5 | 2 | 2017-12-10 |
---------------------------------
6 | 3 | 2018-02-10 |
---------------------------------
7 | 2 | 2018-01-10 |
---------------------------------
9 | 4 | 2018-03-10 |
---------------------------------
10 | 4 | 2018-02-10 |
---------------------------------
11 | 5 | 2018-02-10 |
---------------------------------
car_model_manufacturer_relation
-------------------------------------------------
id | model_id | manufactu_id| service_status |
-------------------------------------------------
1 | 1 | 1 | 1 |
-------------------------------------------------
2 | 5 | 1 | 1 |
-------------------------------------------------
3 | 2 | 2 | 1 |
-------------------------------------------------
4 | 4 | 1 | 1 |
-------------------------------------------------
5 | 2 | 2 | 1 |
-------------------------------------------------
6 | 3 | 3 | 1 |
-------------------------------------------------
I need to update car_model_manufacturer_relation.service_status = '0'
where car_service_tbl.service_date < "2018-03-01".
In this case car_model_manufacturer_relation.service_status of models 2, 3 and 5 should be set to '0' because every car_service_tbl.service_date for these models is smaller than "2018-03-01".
However, for models 1 and 4 car_model_manufacturer_relation.service_status should stay '1' because even that they have records smaller than "2018-03-01" they also have bigger dates ex. "2018-03-10".
I am trying to create a query for this but until now without success.
You'll need to nest a grouped query, to get the MAX date per model, and update from that.
update car_model_manufacturer_relation as cmmr,
(select model_id, max(service_date) as check_date
from car_service_tbl
group by model_id) as cst
set cmmr.service_status = '0'
where cmmr.model_id = cst.model_id
and cst.check_date < "2018-03-01"
Where you're using more than one table and the table names include underscores, I try and alias the tables to make the code a little shorter and easier on the eye, hence the use of cmmr and cst as table aliases.
The MAX date has also been renamed for clarity as check_date. You can of course name this anything you wish.
With sub query:
UPDATE car_model_manufacturer_relation c
LEFT join (SELECT model_id, service_date FROM car_service_tbl ORDER BY service_date DESC LIMIT 1) as s ON s.model_id = c.model_id
SET service_status=0
WHERE c.service_date < "2018-03-01"
#tyro - be careful with your solution, as a LEFT JOIN would update the service status to 0 when there wasn't a service date within the car_service_tbl. You would need to use a full join, rather than just the LEFT JOIN as you suggested in order to update the records correctly I feel.

Getting rows which have a certain revision_status

I am using mysql and I have two tables:
Product Table:
| id | name | prices | revision_id |
|----|-----------|--------|-------------|
| 1 | Produkt 1 | 10 | 1 |
| 2 | Produkt 1 | 4 | 2 |
| 3 | Produkt 1 | 2 | 3 |
| 4 | Product 2 | 42 | 4 |
| 5 | Produkt 2 | 43 | 5 |
| 6 | Produkt 3 | 78 | 6 |
Each product has had price changes. That is why the name is still the same, but the products have a different price.
Revisions Table:
| id | revision_status |
|----|-----------------|
| 1 | 1 |
| 2 | 0 |
| 3 | 0 |
| 4 | 1 |
| 5 | 0 |
| 6 | 1 |
Inside the revision table, 0 indicates an open change, not approved change. 1 indicates - closed - an approved change.
Expected Result:
| id | name | prices | revision_id | revision_status |
|----|-----------|--------|-------------|-----------------|
| 1 | Produkt 1 | 10 | 1 | 1 |
| 2 | Produkt 1 | 4 | 2 | 0 |
| 3 | Produkt 1 | 2 | 3 | 0 |
| 4 | Product 2 | 42 | 4 | 1 |
| 5 | Produkt 2 | 43 | 5 | 0 |
Basically I want all products that have revisions - a revision_status of 0 on it, to see which products actually have revisions.
For example.: Product 3 does not have any price changes, so it should not appear in the final result.
I tried the following:
select *
from product
JOIN revisions
on product.revisions_id = revisions.id
ORDER
BY product.name
However, I still get Product 3 in my table and I am not sure how to get all products that have a revision_status of 0 on it.
I highly appreciate your replies!
In my interpretation you are looking for the products which have more than one revision. Filtering only on revision_status = 0 would not produce your expected result. The following query may answer your question (looking for those products which have more than 1 revision):
SELECT *
FROM product AS p
INNER JOIN revisions AS r ON p.revision_id = r.id
WHERE p.name IN (
SELECT p.name
FROM product AS p
INNER JOIN revisions AS r ON p.revision_id = r.id
GROUP BY p.name
HAVING COUNT(r.revision_status) > 1)
ORDER BY p.name
This would produce your expected result. See example at sqlfiddle.

Mysql: Select tables have different relations and get a result even some of them missing

There is 4 tables that apresent products, sub_cat_products, sold_products and sub_sold some of products dosn't have sub_cat_products then when I join them using Where and AND I miss the product is sold that hasn't sub_cat_products.
how can solve it?
tables:
products
========
id_product | desc1
1 | product 1
2 | product 2
sub_cat_products
========
id_sub_cat_products | id_product | desc2
1 | 1 | sub_product 1
2 | 1 | sub_product 1
3 | 1 | sub_product 3
sold_products
========
id_sold_products | id_product | qtd
1 | 1 | 100
2 | 1 | 50
sub_sold
========
id_sub_sold | id_sold_products | id_sub_cat_products | qtd
1 | 1 | 1 | 30
2 | 1 | 2 | 40
3 | 1 | 3 | 30
Mysql:
SELECT * form products,sub_cat_products, sold_products, sub_sold
WHERE sold_products.id_sold_products = sub_sold.id_sold_products
AND sold_products.id_product = products.id_product
AND sub_cat_products.id_sub_cat_products = sub_sold.id_sub_cat_products

mysql getting data and looking it up in another table

I've got two tables in my database. Table 1 is a list of "timelines" and their corresponding owners and title.
Table 2 is a list of users who have access to the timelines but are followers, not owners.
I'm trying to write a query that outputs the lineID's and corresponding titles that are linked to a userID in either of the two tables.
A query for userID 1 would ideally output:
1 a
2 b
3 c
6 f
Hopefully this isn't too confusing but the purpose is to fill a dynamically generated select box with the LineID and Title for a given UserID...
Table 1 ("owners")
--------------------------
| LineID | UserID | Title |
| 1 | 1 | a |
| 2 | 1 | b |
| 3 | 1 | c |
| 4 | 2 | d |
| 5 | 2 | e |
| 6 | 1 | f |
--------------------------
Table 2 ("followers")
----------------------------
| RowID | LineID | UserID |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 3 | 1 |
| 4 | 3 | 2 |
| 5 | 2 | 2 |
| 6 | 6 | 1 |
----------------------------
I tried using:
SELECT title
FROM `lines`
LEFT JOIN follow
ON follow.user_id = lines.user_id
WHERE follow.user_id = 1
That ended up producing duplicate rows.
The output I need would ideally be an array consisting of all the lineID's and Titles associated with that userID.
select LineId, Title
from owners
where LineId in (select LineId from followers group by LineId )
order by owners.LineId