Multiple aggregate function for mysql. like SUM [duplicate] - mysql

This question already has answers here:
Get rows product (multiplication)
(3 answers)
Closed 9 years ago.
How can I multiple numbers from table to one result like SUM() does?
In "table1" I have column "number1" with these values:
table1.number1
--------------
1
2
3
I try this sql:
SELECT #multiple := #multiple number1 as mul
FROM table1
and I got this:
mul
---
1
2
6
But I need just the last row with the value: 6
without using ORDER DESC

Try this:
SELECT MAX(#multiple := #multiple * number1) AS mul
FROM table1, (SELECT #multiple:=1) a;

Related

to Search 11 in [1,2,10,11,111,112] by SQL(mysql) [duplicate]

This question already has answers here:
How to search JSON array in MySQL?
(11 answers)
Closed 2 months ago.
I have a column in a table that contains some number offsets like this:
my_offsets
[1,2,10,11,111,112]
I want query this rows for search value 11 in [1,2,10,11,111,112]. How can I do in SQL(mysql)?
if data is table
SELECT my_offsets
FROM my_tab
WHERE my_offsets='11'
if data is JSON
SELECT * from my_table
WHERE JSON_CONTAINS(yur_data, '11', '$');
example
JSON_CONTAINS('[1,2,3]','3','$') Returns: 1
JSON_CONTAINS('[1,2,3]','7','$') Returns: 0

How to merge the result of two select mysql query [duplicate]

This question already has answers here:
How can I get multiple counts with one SQL query?
(12 answers)
Closed 1 year ago.
my first query is
select GEAR,count(GEAR)
from new_failure
where STN_CODE = "BVH" group by(Gear);
and its result is
result if image is not visible
# GEAR Total
SIGNAL 8
POINT 16
HASSDAC 5
,SIGNAL 1
SSDAC 1
TRACK CIRCUIT 9
UFSBI 2
DC 1
2nd query
select GEAR,count(GEAR)
from new_failure
where STN_CODE = "BVH"
and MONTH(fail_time) = 4
group by(Gear);
result
# GEAR April
SIGNAL 3
POINT 4
HASSDAC 1
,SIGNAL 1
SSDAC 1
i want result in the form given in image below
You can use either LEFT JOIN, RIGHT JOIN or JOIN depending on what you are aiming to get,
SELECT *
FROM ( select GEAR,count(GEAR)
from new_failure
where STN_CODE = "BVH" group by(Gear) AS A
JOIN ( select GEAR,count(GEAR)
from new_failure
where STN_CODE = "BVH"
and MONTH(fail_time) = 4 AS B
ON A.orders_id=B.orders_id
or you can refer to this link for a similar question
joining two select statements

How to select rows that contain specific number in MySQL? [duplicate]

This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 4 years ago.
I have this table and want to select rows that contain exactly "22".
id field
1 22
2 22,24,78
3 1,22,347
4 2,21,22
5 22,222
Select above rows, not below.
6 222
7 21,23
8 220,322
The REGEXP operator comes in handy here:
SELECT *
FROM yourTable
WHERE field REGEXP '[[:<:]]22[[:>:]]';
We can also try using FIND_IN_SET:
SELECT *
FROM yourTable
WHERE FIND_IN_SET('22', field) > 0;
If all else fails, we can use LIKE, but it takes slightly more heavy lifting:
SELECT *
FROM yourTable
WHERE CONCAT(',', field, ',') LIKE '%,22,%';
But in general, it is bad practice to store CSV (comma separated values) in your database tables. It would be better to store each field value on a separate rows, e.g. use this:
id field
1 22
2 22
2 24
2 78
...
select * from where field like '%22%';

Sort varchar column in mySql [duplicate]

This question already has answers here:
MySQL 'Order By' - sorting alphanumeric correctly
(20 answers)
Closed 7 years ago.
I am trying to sort one column having values
FMOL1001,
FMOL1004,
FMOL1009,
FMOL10010,
FMOL1003,
FMOL10025
But it is not sorting properly,please help
Try this, but im not sure...
If you made a numeric string like this FMOLXXXX-->>XXX then you can sort XXX:
SELECT column, SUBSTRING(column FROM 5) sort FROM table ORDER by CAST(sort AS UNSIGNED)
Sample data :
id name
-------------
1 FMOL1001
2 FMOL1004
3 FMOL1009
4 FMOL10010
5 FMOL1003
6 FMOL10025
Query :
SELECT id, name
FROM table
ORDER BY LPAD(SUBSTR(name,5,LENGTH(name)-4),10,'0') ASC
Output :
id name
-------------
1 FMOL1001
5 FMOL1003
2 FMOL1004
3 FMOL1009
4 FMOL10010
6 FMOL10025
Explanation :
LPAD(SUBSTR(name,5,LENGTH(name)-4),10,'0')
Will produce :
FMOL0000001001
FMOL0000001003
FMOL0000001004
FMOL0000001009
FMOL0000010010
FMOL0000010025

SQL check in two tables [duplicate]

This question already has answers here:
SELECT * WHERE NOT EXISTS
(5 answers)
Closed 8 years ago.
Hope you can help me.
I have 2 tables in my MqSQL database.
Table 1.
speedywebs_data
cardid
1
2
3
4
Table 2:
Speedywebs_results
resultid / card1 / card2
1 / 2 / 1
2 / 4 /4
my problem is, i want to get all posts in speedywebs_data table, but only cardid's whos not listed in the speedywebs_results cardid1. How can i do that?
SELECT speedywebs_data.*
FROM
speedywebs_data
WHERE
cardid NOT IN (SELECT card1 FROM Speedywebs_results WHERE card1 IS NOT NULL)
or you could use also this:
SELECT speedywebs_data.*
FROM
speedywebs_data LEFT JOIN Speedywebs_results
ON speedywebs_data.cardid = Speedywebs_results.card1
WHERE
Speedywebs_results.card1 IS NULL
You wanna check if a value doesn't exist ?
So use... NOT EXISTS.
select cardid
from speedywebs_data swd
where not exists (select null
from speedywebs_results swr
where swr.card1 = swd.cardid)