Check equality of n numbers in mysql - mysql

I need to check whether all numbers are the same. The values are coming from different columns. The signiture should allow to put any amount of columns (like the COALESCE(...) method)
SELECT equality(42, 42, 42)
should return true and
SELECT equality(23, 42, 133)
should return false.
Is there a nice way to code this?
At time i did it like this:
SELECT (x1 = x2 AND x2 = x3);
But i hope there is a more elegant way.

Use this:
SELECT GREATEST(42, 42, 42) = LEAST(42, 42, 42)

Related

MS Access Case sensitive query giving incorrect result

Why do these queries give different results? Reference is a single character column and I would expect to have a result giving counts for upper and lower case letter 'r'.
Select SUM(IIF(StrComp([REFERENCE],'R',0) = 0, 1, 0)) AS BIG_R,
SUM(IIF(StrComp([REFERENCE],'r',0) = 0, 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
The result is that both BIG_R and LITTLE_R are the same and equal the count of BIG_R's
However,
Select SUM(IIF(StrComp([REFERENCE],'r',0) = 0, 1, 0)) AS LITTE_R,
SUM(IIF(StrComp([REFERENCE],'R',0) = 0, 1, 0)) AS BIG_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Again LITTLE_R and BIG_R are the same, but this time they equal the count of LITTLE_R's
This looks like a bug in the way MS Access processes this type of query, or have I missed something here?
Access (or probably rather JetEngine) thinks that StrComp is called twice with the same argument and optimizes away one of the two calls.
A workaround is to compare the ASCII character values (Asc("r") = 114, Asc("R") = 82):
Select
SUM(IIF(Asc([REFERENCE]) = Asc('R'), 1, 0)) AS BIG_R,
SUM(IIF(Asc([REFERENCE]) = Asc('r'), 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Yet another workaround:
Select SUM(IIF(StrComp([REFERENCE],Chr$(82),0) = 0, 1, 0)) AS BIG_R,
SUM(IIF(StrComp([REFERENCE],Chr$(114),0) = 0, 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Here the two inputs to StrComp are clearly different. So, the second call not optimized away.

mysql coupled OR statement

I'm trying to do a coupled OR statement
select * from table where cat = "x" OR (cat = "y" AND rand()<=0.25);
So I would like to select all items with cat = x or cat = y, but only 25% of y. The statement above gives med 25% of both x and y.
How can I seperate my statement so this can be done?
Thanks
I don't know what you are doing but i can almost smell that this is a very dodgy way to implement SQL.
I assume, you want to change your where clause if random number generates a result with %25 probability.You can not do this with ORs if cat="x" then it will not even try to look at Y. That gives u all Xs and quarter of Ys.
Try this for implementation of probablity check.
IF (RAND() <= 0.25)
{ sql_statement_forY }
[ ELSE
{ sql_statement_forX } ]

Mask integer field in mysql

I need to mask integer field in mysql such that 9999911111 becomes 9900001111. I want to keep first 2 digits and last 4 digits and need to mark rest of the digits as 0 for the integers stored in the field.
I have created a query and it's working but I am not sure whether this is right way to do for integers or not.
update table_name
set field_name=CONCAT(SUBSTR(field_name, 1, 2),
REPEAT('0', CHAR_LENGTH(field_name) - 6),
SUBSTR(field_name, CHAR_LENGTH(field_name)-3, CHAR_LENGTH(field_name)));
Just trying a different approach .
SET #myVar = 344553543534;
SELECT #myVar - (SUBSTRING(#myVar, 4, LENGTH(#myVar) - 7) * 10000) ;
Above mentioned formula will give 344000003534 as the result. Tried with different combination and found it working.
So your query need to change as given below
UPDATE table_name
SET field_name=
(field_name - (SUBSTRING(field_name, 4, LENGTH(field_name) - 7) * 10000));
Explanation :
Consider Number, a = 344553543534;
Expected Result, b = 344000003534;
c = (a - b) = 344553543534 - 344000003534 = 553540000;
Now if you consider the result, c, 55354 is the numbers where masking required, and 0000 indicates the last 4 number to be left open.
So to get masked value, we can use the formula, b = a - c;
So now to get c, used SUBSTRING(a, 4, LENGTH(a) - 7) * 10000
EDIT : To keep only first two numbers, use 3 instead of 4 and 6 instead of 7. I assumed that you needed to keep first 3.
SET #myVar = 344553543534;
SELECT #myVar - (SUBSTRING(#myVar, 3, LENGTH(#myVar) - 6) * 10000) ;

mysql_fetch_fields returns different length then expected

I need to create table in other database based on select result types. Query results can map to actual columns in table or not f.e. Select 1, c from char_length_test.
How to get actual column size after select statement using mysql C api?
I have created such table:
CREATE TABLE char_length_test (c char(22))
And using mysql_real_query to execute this query
SELECT c from char_length_test
Right after that I execute mysql_fetch_fields to get length of c field and expect it to be 22 as in create table statement. Unfortunately length contains value of 66 (3 times more then I expect). Tried different sizes but result is the same, length is always 3 times bigger.
Also used gdb to see if there is any other data field containing expected value:
{
name = "c",
org_name = "c",
table = "char_length_test",
org_table = "char_length_test",
db = "database",
catalog = "def",
def = 0x0,
length = 66,
max_length = 0,
name_length = 1,
org_name_length = 1,
table_length = 16,
org_table_length = 16,
db_length = 6,
catalog_length = 3,
def_length = 0,
flags = 0,
decimals = 0,
charsetnr = 33,
type = MYSQL_TYPE_STRING,
extension = 0x0
}
mysql_fetch_fields() and mysql_field_len() will return a number of bytes required to store a VARCHAR value, not the number of characters. For UTF-8 columns, this will return 3 times the actual column size, even though the documentation says otherwise. This is so that your C code will know how much memory to allocate. If you set your MySQL connection (not the table structure!) to a different character set, you will get different results.
Edit:
You can change the character set of a MySQL connection by calling mysql_set_character_set(). If you use an 8bit character set, you should get a number of bytes that matches the width of the database column, e.g.:
mysql_set_character_set(&mysql,'latin1');

how to apply zeroes to non-matching indices of filtered data

I am learning octave, and I have a comparison made like so:
ma = [1,2,3,4,5];
indices = ma > 3;
The conditions filter 'ma', and the var 'indices' prints the indices matching the conditions, which looks like:
[4, 5]
... but what I want is to use that result to return an array like this:
[0,0,0,4,5];
Is there a function for this?
You can do the following:
ma = [1,2,3,4,5];
ma(ma <= 3) = 0
Basically, just invert the condition and use it to set the values to zero.