need help to shorten mysql multiple OR conditions - mysql

I have a table which contains manager id and staff ids (comma separated) and i have the following sql statement:
SELECT manager_id, staff_ids
FROM manager_staffs
WHERE 1 IN (staff_ids)
OR 3 IN (staff_ids)
OR 5 IN (staff_ids)
OR 23 IN (staff_ids)
OR 12 IN (staff_ids)
OR 16 IN (staff_ids)
OR 19 IN (staff_ids)
OR 32 IN (staff_ids)
OR 123 IN (staff_ids)
............
basically, it is a very long query with a lot of 'OR' condition. I would like to improve or shorten it if there is a way.
need help from all the sql gurus.

The problem is your data structure - don't put all the staff ids in one column. Make a table - you can call it manager_staff:
CREATE TABLE manager_staff (
manager_id int,
staff_id int
);
And so you can make records that show which manager every staff person reports to. Then your query becomes:
select manager_id, staff_id from manager_staff where staff_id in (1,3,...);

Im not sure what are you trying to achieve but try this:
SELECT manager_id, staff_ids
FROM manager_staffs
WHERE staff_ids in ('1','3' and so on)

Assuming staff_ids is a comma separated field then you could do the following.
SELECT DISTINCT manager_id, staff_ids
FROM manager_staffs a
INNER JOIN (SELECT 1 AS staff_id UNION SELECT 3 UNION SELECT 5 UNION SELECT 23 UNION SELECT 12 UNION SELECT 16 UNION SELECT 19 UNION SELECT 32 UNION SELECT 123) b
ON FIND_IN_SET(b.staff_id, a.staff_ids) > 0
But as I commented earlier, splitting it up would be a far better idea

why dont you try this
SELECT manager_id, staff_ids
FROM manager_staffs
WHERE staff_ids IN (1,3,5,23,12,16,19,32,123,.......)
you have no choice how to reduce your ORs only if you create other table and select them like query above.
structure your table is best to get performance when retrieving data.
your table should look like that
manager_staff
manager_id , staff_id
1 1
1 3
1 5
1 23
..........

Related

PHP/SQL Query: combine two tables with only unique records and count saturdays

I have two MySQL tables, called "accounts" and "events".
Accounts
ID
name
1
Pete
2
Josh
3
Harry
Events
ID
date
account_id
1
2021-10-11
1
2
2021-10-23
1
3
2021-10-23
2
4
2021-11-06
1
The account_id in the events table is linked to the id in the accounts table.
My question is: which query can I use to count only saturdays (date YYYY-mm-dd format) for each unique user in the accounts table? So I get the next result:
Name
Saturdays
Josh
1
Pete
2
Harry
0
I've tried many queries (with i.e. the (inner) JOIN, DISTINCT and GROUP BY keywords) but I don't get the exact result. Can you please help me?
Many thanks in advance!
I think something like this should work (untested):
SELECT
`a`.`name` AS `Name`,
COUNT(`e`.`ID`) AS `Saturdays`
FROM `accounts` AS `a`
LEFT JOIN `events` AS `e` ON `e`.`account_id` = `a`.`ID` AND WEEKDAY(`e`.`date`) = 5
GROUP BY `a`.`ID`

Better way to use UNION

i have a table products that have 5 fields to supplier
code supplier_1 supplier_2 supplier_3
000001 3 87 25
000002 12 5 48
I have to bring the code of product and all suppliers that have relation with
For that i create a query of each supplier and relate then
like
select code supplier1
from products
union
select code supplier2
from products
... and so on
there is another way to do that ?
Starting in MySQL 8.0.14, you can use a lateral join:
select t.code, s.supplier
from t cross join lateral
(select supplier_1 as supplier union all
select supplier_2 as supplier union all
select supplier_3 as supplier union all
select supplier_4 as supplier
) s;

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'...

MySQL Group by week num w/ multiple date column

I have a table with columns similar to below , but with about 30 date columns and 500+ records
id | forcast_date | actual_date
1 10/01/2013 12/01/2013
2 03/01/2013 06/01/2013
3 05/01/2013 05/01/2013
4 10/01/2013 09/01/2013
and what I need to do is get a query with output similar to
week_no | count_forcast | count_actual
1 4 6
2 5 7
3 2 1
etc
My query is
SELECT weekofyear(forcast_date) as week_num,
COUNT(forcast_date) AS count_forcast ,
COUNT(actual_date) AS count_actual
FROM
table
GROUP BY
week_num
but what I am getting is the forcast_date counts repeated in each column, i.e.
week_no | count_forcast | count_actual
1 4 4
2 5 5
3 2 2
Can any one please tell me the best way to formulate the query to get what I need??
Thanks
try:
SELECT weekofyear(forcast_date) AS week_forcast,
COUNT(forcast_date) AS count_forcast, t2.count_actual
FROM
t t1 LEFT JOIN (
SELECT weekofyear(actual_date) AS week_actual,
COUNT(forcast_date) AS count_actual
FROM t
GROUP BY weekOfYear(actual_date)
) AS t2 ON weekofyear(forcast_date)=week_actual
GROUP BY
weekofyear(forcast_date), t2.count_actual
sqlFiddle
You have to write about 30 (your date columns) left join, and the requirement is that your first date column shouldn'd have empty week (with a count of 0) or the joins will miss.
Try:
SELECT WeekInYear, ForecastCount, ActualCount
FROM ( SELECT A.WeekInYear, A.ForecastCount, B.ActualCount FROM (
SELECT weekofyear(forecast_date) as WeekInYear,
COUNT(forecast_date) as ForecastCount, 0 as ActualCount
FROM TableWeeks
GROUP BY weekofyear(forecast_date)
) A
INNER JOIN
( SELECT * FROM
(
SELECT weekofyear(forecast_date) as WeekInYear,
0 as ForecastCount, COUNT(actual_date) as ActualCount
FROM TableWeeks
GROUP BY weekofyear(actual_date)
) ActualTable ) B
ON A.WeekInYear = B.WeekInYear)
AllTable
GROUP BY WeekInYear;
Here's my Fiddle Demo
Just in case someone else comes along with the same question:
Instead of trying to use some amazing query, I ended up creating an array of date_columns_names and a loop in the program that was calling this query, and for each date_column_name, performing teh asme query. It is a bit slower, but it does work

MySql Result Set Combine Columns

I have this result set from my query:
OrderId CustomerId ProducerId CustomerPayment ProducerPayment
1 1 3 10 5
1 1 4 10 5
1 2 3 10 5
1 2 4 10 5
I need to return this result into this:
OrderId UserId Payment
1 1 20
1 2 20
1 3 10
1 4 10
Just combining the CustomerId and ProducerId into UserId. Same with the Payment Columns.
Is there any way to achieve this with using just a simple select and group by? I'm avoiding temp tables, calling multiple same queries and like for optimization. I hope this is possible.
Thanks a lot
SELECT
OrderId,
CustomerID AS UserId,
SUM (CustomerPayment) As Payment
FROM orders
UNION ALL
SELECT
OrderId,
ProducerId AS UserId,
SUM (ProducerPayment) As Payment
FROM orders
Try something like this:
select
OrderId,
CustomerId,
sum(CustomerPayment) Payment,
group_concat(OrderId separator ',') listOrders /* list all OrderID's from the user and separates these with a , */
from your_table
group by CustomerId
Dont know how you query looks like atm?