what is mysql query for below four tables - mysql

I have four tables.
User:
user_id user_name client_id
1 abhi 1
2 ravi 2
Client:
client_id client_name products
1 tom cake, patties
2 pet cookie, cake
Products:
product_id product_name
1 cake
2 cookie
3 patties
Report:
report_id product_id
1 1
2 3
3 2
4 1
5 3
If Ravi login then he only able see report like below:
report_id product_id
1 1
3 2
4 1
I need only MySQL Query

In your "Client" table instead of saving multiple values of "product" split it and put only "product id" rather than actual product value that's why you have table "product". Your table "client" is not in First normal form. So if possible, change the structure of table

I agree with Simemon's answer, you need to normalize your DB tables.
Then also if you want to go with this table structure only, you can try this.
select * from reports where product_id in (select product_id from products where product_name in (select products from clients where client_id = (select client_id from users where username = 'ravi' limit 1)))
I have not tested this. let me know if it gives any error, I will check it.

Related

Inserting json data into query

I'm looking for help for one of my previous coding challenge, so I have two table, the first one is Sales and the second one is Product. Which look something like this :
Sales table:
ord_id item_id cust_id status qty total_price order_date
---------------------------------------------------------------------------------
1 A308 CS209 Sold 1 30000 10-10-21
2 A405 CS209 Cancel 4 44000 10-18-21
3 B476 CS890 Sold 8 6400 10-29-21
and Product table
item_id item_category item_price item_name date_created date_updated
----------------------------------------------------------------------------------------
A308 Electronic 30000 TV 08-10-21 10-10-21
A405 Furniture 11000 Chair 12-25-20 10-18-21
B476 Misc 800 Cutter 05-11-21 10-29-21
I was told to write a query to get a result set like this:
order_date detail total_customer total_price
-------------------------------------------------------------------------------------------
10-10-21 {"item_id":"A308","item_category":...} 1 30000
10-29-21 {"item_id":"B476","item_category":...} 1 6400
The other parts are easy but I'm still confused on how to turn certain columns from the Product table into JSON and inserting that json into the Detail column as shown above.
I've done something like this to get the first part :
SELECT order_date, COUNT(cust_id) as total_customer, SUM(total_price) as total_price
FROM Sales
WHERE status = "sold"
GROUP BY order_date
Can anyone help me how to do this in a single query?

Query to get distinct count of column with MYSQL

I have a table name POEMS with columns poemID and userID. I want to make the poemID distinct and show only the count in the userID column. There are 3 poemID's with 1. I want to make it show poemID 1 and userID would be 2. poemID 2 and userID 3, for the 3 rows and poemID 3 with userID 1 for the 1 row.
|poemID | userID|
1 1
1 5
2 2
2 5
2 4
3 2
I want the above table to look like the table below.
|poemID | userID |
1 2
2 3
3 1
My SQL query im trying is below but its not working. Please help.
SELECT DISTINCT(poemID), COUNT(userID) FROM POEMS GROUP BY poemID;
This looks like a straight aggregation query:
SELECT poemID, COUNT(*) no_users
FROM POEMS
GROUP BY poemID;
Or, if the same user may appear multiple times for a given poem and you want to count it only once:
SELECT poemID, COUNT(DISTINCT userID) no_distinct_users
FROM POEMS
GROUP BY poemID;

Mysql query IN clause

I have two tables.
One is where the price of ice cream is saved in different currency and another where which ice cream for the respective currency id is added by user to buy.
icecreamprice
id icecream_id name currency_id price
1 3 white_chocolate 2 3
2 3 white_chocolate 4 10
3 11 dark_chocolate 2 6
4 3 white_chocolate 3 4
Here id is pk and auto increment, icecream_id is being saved from a table where ice cream names and their respective id is mentioned.
cart
id icecream_id user_id price_id(type=TEXT)
1 3 111 1,4
So i want to fetch everything from both icecreamprice and cart table when icecreamprice_id is present in cart_price_id and cart user_id is 111.
so this is my query
"SELECT id,icecream_id,name,price,c.user_id FROM icecreamprice ic,cart c WHERE c.user_id=111 and ic.id IN (c.price_id)"
But seems like this query is not effective, its fetching only one row insteam of two rows from first table
You can't use IN on a text string in MySQL, but you can use FIND_IN_SET instead:
SELECT ic.id, ic.icecream_id, ic.name, ic.price, c.user_id
FROM icecreamprice ic
JOIN cart c ON FIND_IN_SET(ic.id, c.price_id)
WHERE c.user_id=111
Output:
id icecream_id name price user_id
1 3 white_chocolate 3 111
4 3 white_chocolate 4 111
Note that I have made your JOIN explicit and added the condition to the JOIN for clarity.
Demo on dbfiddle

MYSQL for selecting values from table and show with comma separator

I have two tables:
sales Table
===============================
id cust_id total_price
===============================
1 1 1000
2 2 1500
sales_item Table
======================================================
id sales_id cust_id product quantity
======================================================
1 2 2 pen 2
2 2 2 pencil 3
3 1 1 book 2
4 1 1 pencil 2
I need to query these two tables inorder to get the following result:
=========================================
sales_id cust_id product
=========================================
2 2 pen,pencil
1 1 book,pencil
Can anyone help me to query these 2 tables inorder to get the above result??
I tried using GROUP_CONCAT. Hers is what I have tried:
SELECT s.id,s.cust_id,s.total_price,
GROUP_CONCAT(i.prdt_name)products
FROM sales s
JOIN sale_items i ON s.id=i.sales_id
And the result I got is:
======================================================
id cust_id total_price product
======================================================
2 2 1500 pen,pencil,book
This is the not the result am expecting..
Try something like this, consider that this is not tested, it may help you.
select sales.id,sales.cust_id, concat(sales_item.product)
from sales LEFT JOIN sales_item ON sales_item.sales_id = sales.id
group by sales.id
I got the answer by using GROUP_CONCAT itself. Here is my Query:
SELECT s.id,s.cust_id,s.total_price,
GROUP_CONCAT(i.prdt_name)products
FROM sales s
JOIN sale_items i ON s.id=i.sales_id
GROUP BY s.id

SQL Table Solution With Calculating Different Columns in Same Table

I have a sql table like :
id buy_product buy_product_total sell_product sell_product_total
1 apple 5
2 banana 8
3 cake 20
4 apple 1
5 cake 2
6 apple 2
My problem is, I want to show product name and how many product left. Like :
product_name left
apple 6
cake 18
How can I show like that solution with sql query ?
I create table as answerers as :
Buy Table
id product_name total
1 apple 5
2 banana 8
3 cake 20
4 apple 2
Sell Table
id product_name total
1 apple 1
2 cake 2
I want to table like this
product_name left
apple 6
banana 8
cake 18
Is not a good table, could be better that buy and sell to be the same collumn buy with positive values and sell with negative.
But answer your question, suppose that your table name is myTable,
obs: you can execute every select separeted to understand better
select buy_product as product_name, (buy_total - sell_total) as left
from (
(select buy_product, sum(buy_product_total) as buy_total
from myTable where buy_product_total is not null group by buy_product) as buy_list
inner join
(select sell_product, sum(sell_product_total) as sell_total
from myTable where sell_product_total is not null group by sell_product) as sell_list
on buy_list.buy_product = sell_list.sell_product
)
As others have noted, your table structure is less than optimal.
However, given what you have, this will give you the results you're after.
select product, sum(total) from
(
select buy_product as product, buy_product_total as total
from yourtable
where buy_product is not null
union
select sell_product, -sell_product_total
from yourtable
where sell_product is not null
) v
group by product
Or, with your two tables
select product_name, sum(total) from
(
select product_name, total
from buy_table
union
select product_name, -total
from sell_table
) v
group by product_name
You should consider a different database design that is more appropriate (You may want to read up on normalization), but query follows:
SELECT t1.buy_product_total - t2.sell_product_total
FROM ProductTable t1, ProductTable t2
WHERE t1.buy_product = t2.sell_product
i.e. You're joining the table to itself using a 'self join'...