What is the difference between union and union all in mysql? [duplicate] - mysql

This question already has answers here:
What is the difference between UNION and UNION ALL?
(19 answers)
Closed 9 years ago.
Can anyone please tell me the difference where to use union and where to use uinon all in mysql

UNION gets distinct values, UNION ALL doesn't.

Related

Is this possible for sum(base_total - total_discount) in Laravel [duplicate]

This question already has answers here:
Laravel 5.7 search by date
(2 answers)
Closed 1 year ago.
My SQL query is
SELECT SUM(base_total - total_discount)
FROM orders
WHERE created_at LIKE '2020-09-18%'
AND customer_id = 9
so how to write this query in laravel
My Laravel query for this is Order::where('created_at','like','2020-09-18%','and','customer_id','=','9')->sum('base_total','-','total_discount'); where Order is model

DATE_TRUNC function working in postgres but not working in MYSQL and [duplicate]

This question already has answers here:
Select only year from MySql
(3 answers)
Closed 4 years ago.
I'm trying to leverage 'date_trunc' function in a query and it returns the below error
FUNCTION database.date_trunc does not exist.
Please advice
You could use DATE_FORMAT(created_at ,'%Y-01-01') to get first day of year:
SELECT DATE_FORMAT(created_at ,'%Y-01-01') AS `year`, COUNT(*) AS cnt
FROM sales_order
GROUP BY DATE_FORMAT(created_at ,'%Y-01-01')
ORDER BY `year`;
DATE_TRUNC is supported by PostgreSQL.

MYSQL cumulative sum by date [duplicate]

This question already has answers here:
Calculate a running total in MySQL
(5 answers)
Closed 4 years ago.
How can i do cumulative sum?
My query is :
select dateb,ricetype,bajet1
from bajet
where ricetype='grand total'
How do i archieve 'New Value'?
You can do it like below :
set #CumSum := 0;
select dateb, ricetype, bajet1, (#CumSum := #CumSum + bajet1) as New_Value
from bajet where ricetype='grand total';

Truncate on a select [duplicate]

This question already has answers here:
Format number to 2 decimal places
(6 answers)
Closed 7 years ago.
I have the following query:
SELECT
SUM(`SPEND_AMOUNT`) AS 'Total Spend Amount',
SUM(`SPEND_AMOUNT`) / COUNT(DISTINCT `INVOICENUMBER`) AS 'Average Spend Amount'
FROM
`TABLE`
I would like to truncate(?) the results for "Average Spend Amount" to two decimal places.Thanks for the help.
Round(SUM(SPEND_AMOUNT) / COUNT(DISTINCT INVOICENUMBER), 2)

MySQL select Display Zero when group by month or week [duplicate]

This question already has answers here:
Mysql to select month-wise record even if data not exist
(2 answers)
Closed 9 years ago.
I have a table with a column date and a column ID (to keep it simple). I am doing a query to count the total and group by week
SELECT MONTH(table1.Date_1st), YEAR(table1.Date_1st), COUNT(table1.Id)
FROM table1 as table1
WHERE Date_1st BETWEEN '2013-04-01' AND '2013-07-25'
GROUP BY WEEK(Date_1st)
the problem is there is no results on specific week the result is not taken into consideration. I already try ifnull(table1.Id,0)
Try ISNULL or other NULL SQL functions:
NULL functions
ISNULL returns a 0 if the value is null.