Checksum of SELECT results in MySQL - mysql

Trying to get a check sum of results of a SELECT statement, tried this
SELECT sum(crc32(column_one))
FROM database.table;
Which worked, but this did not work:
SELECT CONCAT(sum(crc32(column_one)),sum(crc32(column_two)))
FROM database.table;
Open to suggestions, main idea is to get a valid checksum for the SUM of the results of rows and columns from a SELECT statement.

The problem is that CONCAT and SUM are not compatible in this format.
CONCAT is designed to run once per row in your result set on the arguments as defined by that row.
SUM is an aggregate function, designed to run on a full result set.
CRC32 is of the same class of functions as CONCAT.
So, you've got functions nested in a way that just don't play nicely together.
You could try:
SELECT CONCAT(
(SELECT sum(crc32(column_one)) FROM database.table),
(SELECT sum(crc32(column_two)) FROM database.table)
);
or
SELECT sum(crc32(column_one)), sum(crc32(column_two))
FROM database.table;
and concatenate them with your client language.

SELECT SUM(CRC32(CONCAT(column_one, column_two)))
FROM database.table;
or
SELECT SUM(CRC32(column_one) + CRC32(column_two))
FROM database.table;

Related

CONCAT() inside GROUP_CONCAT() with count

I am trying to get results with group_concat, concat and count functions in MySQL but it gives me error.
here is my table
First, when I try to get count and status with concat, it works fine.
$query="SELECT CONCAT(`status`,':',count(status)) FROM `mytable` GROUP BY status"
output:
Hold:2
Completed:3
Cancelled:2
It's all fine till here. Now I want this output in one row. So I tried using GROUP_CONCAT().
$query="SELECT GROUP_CONCAT(CONCAT(`status`,':',count(status)) SEPARATOR ',') as rowString FROM `mytable` GROUP BY status"
but now its giving me error " Invalid use of group functions"
Note: the same query works if I replace count(status) with some other field from table ( without count). The count() function is causing some problem when used in this manner.
Desired Output
Hold:2,Completed:3,Cancelled:2
Appreciate your help.
You cannot nest aggregation functions (count() is in the arguments to group_conat()). One solution is to select from a nested subquery.
SELECT group_concat(status, ':', count SEPARATOR ',') rowstring
FROM (SELECT status,
count(*) count
FROM mytable
GROUP BY status) x;

Why does DISTINCT has to go first in MySQL?

I have a query that works when I do
SELECT DISTINCT(table.field.id), 1 FROM ...
but fails when I do
SELECT 1, DISTINCT(table.field.id) FROM ...
Is this a known behavior?
Why does the first one work while the second doesn't?
Unfortunately I'm not able to add a comment yet.
What #Gordon Linoff has written is exactly right.
You are getting error as DISTINCT in general works as part of SELECT clause or AGGREGATE function. It is used to return unique rows from a result set and it can be used to force unique column values within an aggregate function.
Examples: SELECT DISTINCT * ... COUNT(DISTINCT COLUMN) or SUM(DISTINCT COLUMN).
More information's about DISTINCT in popular DB engines:
PostgreSQL:https://www.postgresql.org/docs/9.0/static/sql-select.html#SQL-DISTINCT
SQL Server: https://www.techonthenet.com/sql_server/distinct.php
Oracle: https://www.techonthenet.com/oracle/distinct.php
MySQL:
https://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html
https://dev.mysql.com/doc/refman/5.7/en/select.html

How can I retrieve the column names from an empty MySQL select query result

Is there a way to retrieve the column names of a query that returns no data?
The result of this query would be empty.
Is there a way how to find the column names when there's no result?
Please note that I'm aware of solutions using DESCRIBE and select column_name from information_schema.columns where table_name='person';
but I need a more flexible solution that will fit these multicolumn queries.
Please also note that I am still using the original PHP MySQL extention (so no MySQLi, and no PDO).
If you wrap your query with the following SQL, it will always return the column names from your query, even if it is an empty query result:
select myQuery.*
from (select 1) as ignoreMe
left join (
select * from myTable where false -- insert your query here
) as myQuery on true
Note: When the results of the subquery are empty, a single row of null values will be returned. If there is data in the subquery it won't affect the output because it creates a cross-product with a single row...and value x 1 = value
Execute following command if the result of your previous query is empty
SHOW columns FROM your-table;
For more details check this.
I'm not sure if it will satisfy you but you can do this
SELECT *, COUNT(*) FROM table;
It will return null values (except last column which you can ignore) if the query is empty and you will be able to access all columns. It's not proper way of doing it and selecting names from INFORMATION_SCHEMA would be much better solution.
Please note that result is aggregated and you need to use GROUP BY to get more results if there are any.
You should ,
Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS
Where TABLE_SCHEMA='yourdb'
AND TABLE_NAME='yourtablename';

How to get minimum value from a set of values returned by SELECT query?

I have written a SELECT query, which will return a set of values, for ex.,
The following one is the actual table -
select data from tab1 where id <5; // This statement returns me the following table
I am trying to get the minimum value of the resultant table. I have tried the following query for that -
select MIN(select data from tab1 where id<5);
SQLite Browser says, there is an error in the select statement. My doubt is, whether we can give a select statement directly into an aggregate function like MIN()? If not, can you please suggest me a better way to do this task?
Thanks in advance.
Try this way:
select MIN(data)
from tab1 where id<5;

MySQL select with subquery having replace

So I have a data with format like ;1;;2; and then I need to use this number in a query so I thought I'd convert it to 1,2 and use that in a IN condition. In my table, the result should return 2 rows but instead it is returning only 1 row.
My query is like this. The subquery return 1,2 with no problem but only 1 row is retrieve.
select *
from wt_lists
where id IN ((select replace (replace(sendto, ';;',','),';','')
from wt_stats where statsid IN (1)))
But when I try it with this. It returns the correct result, which in my case is 2 rows.
select *
from wt_lists
where id IN (1,2)
What am I missing here?
Comma delimited strings need to be explicitly defined in the query in order to be used in the IN clause - there's countless examples on SO where people need to use dynamic SQL to incorporate user submitted comma delimited strings.
That said, I have a solution using the FIND_IN_SET function:
SELECT DISTINCT wl.*
FROM WT_LISTS wl
JOIN (SELECT REPLACE(REPLACE(ws.sendto, ';;',','),';','') AS ids
FROM WT_STATS ws
WHERE ws.statsid = 1) x ON FIND_IN_SET(wl.id, x.ids) > 0
You are replacing the string:
';1;;2;'
To:
'1,2'
So, you SQL query looks like:
select * from wt_lists where id IN ('1,2') from wt_stats where statsid IN (1)
To use IN clause you need select different values in different rows.
I found this store procedure that does exactly what you need.
http://kedar.nitty-witty.com/blog/mysql-stored-procedure-split-delimited-string-into-rows/
I have not tested, but it is the way.
Obs: Like David said in the comments above, parsing the data in your application is a better way to do this.