SQL BINARY Operator not showing exact match - mysql

I have a table with the following columns...
[Name] = [Transliteration] = [Hexadecimal] = [HexadecimalUTF8]
...with multiple rows of UTF-8 characters, such as:
ङ = ṅa = 0919 = e0a499
ञ = ña = 091e = e0a49e
ण = ṇa = 0923 = e0a4a3
न = na = 0928 = e0a4a8
In order to search for the row that exactly matches ña in the Transliteration column , I enter the following command:
SELECT DISTINCT * FROM Samskrta
WHERE BINARY (Transliteration = concat(0xc3b161))
ORDER BY HexadecimalUTF8;
...which produces 4 rows.
Why is the SQL command not producing only the row that exactly matches ña?
What SQL command produces only the row that exactly matches ña?
The following command produces the same results:
SELECT DISTINCT * FROM Samskrta
WHERE BINARY (Transliteration = 'ña')
ORDER BY HexadecimalUTF8;

FIRST OF ALL, your query can't work as indicated: you are applying BINARY() to the result of the logical comparison, NOT comparing the BINARY() of whatever to whatever.
Try reproducing your code PRECISELY if you expect people to be able to tender assistance.

Related

Multiple WHERE conditions using AND => No query result

I have a table with a few thousand entries. And My purpose is to select all entries from all versions that correspond to a given one. And the resulted entries must correspond exactly to the given entry.
But somehow, the SQL query does not work. The original project uses Access 2007. But I have tried also in MySQL and no success
I put here the sql query, but I also made a SQL fiddle:
SELECT
idvalue,
idtag,
iddevice,
idversion,
idtext,
description,
idaccess,
defaultvalue,
minimumvalue,
acceptedvalue,
maximumvalue,
outofrangevalue,
iddatatypepn,
iddatatypeopc,
size,
idresolution,
idunit,
idaccuracy,
enumerationvalues,
comments,
remanentvolatile,
storedatpn,
storedatmain,
`generated`,
edittime
FROM
SomeValues
WHERE
idtag = 2 AND iddevice = 1
AND idtext = 433
AND description = 'Input voltage (AC)'
AND idaccess = 12
AND defaultvalue IS NULL
AND minimumvalue =0
AND acceptedvalue = 5300
AND maximumvalue = 10050
AND outofrangevalue = 11000
AND iddatatypepn = 2
AND iddatatypeopc = 19
AND size = 2
AND idresolution = 2
AND idunit = 1
AND idaccuracy = 2
AND enumerationvalues IS NULL
AND comments IS NULL
AND remanentvolatile IS NULL
AND storedatpn = FALSE
AND storedatmain = FALSE
AND `generated` = TRUE
Fiddle: here
Can you please explain what is wrong with the sql query?
The result should be those 3 entries from the fiddle table.
And yes, I must use all the conditions from the "Where" clause, since the entries can match 90% but also have small differences
You have problem in line:
AND description = 'Input voltage (AC)'
change it to:
AND description = '"Input voltage (AC)"'
and everything will works.
Problem lies in the fact that you searched for text Input voltage (AC) instead of "Input voltage (AC)" (how is stated in column description).

What's wrong with this SQL query WHERE AND clause?

Previously, this was working:
$patient_story_set_photos = $wpdb->get_results('SELECT * FROM wp_before_after WHERE patientID = '.$post->ID.' AND patient_display = 1');
However, when I try to add another AND condition like this:
$patient_story_set_photos = $wpdb->get_results('SELECT * FROM wp_before_after WHERE patientID = '.$post->ID.' AND patient_display = 1 AND period_taken = '.$set->period_taken);
I get the following error on screen:
WordPress database error: [Unknown column '1hour' in 'where clause']
SELECT * FROM wp_before_after WHERE patientID = 8175 AND patient_display = 1 AND period_taken = 1hour
Can't see why there's a problem, are you not allowed to use multiple AND conditions in SQL?
The problem is not the AND, the problem is your 1hour, 1hour unquoted means a reference to an object (database, table) named 1hour, you need to quote '1hour'.
If you write
SELECT * FROM wp_before_after
WHERE patientID = 8175
AND patient_display = 1
AND period_taken = '1hour'
you will compare the field periodtaken to a string (CHAR,VARCHAR,TEXT) equal to '1hour'.
I assume period_taken is a field typed CHAR,VARCHAR or TEXT
Before anything, DO NOT CONCATENATE SQL STRINGS nowadays it is a MUST (see how to do it properly https://stackoverflow.com/a/60496/3771219)
The problem you are facing is because, I presume, that the period_taken field is some sort of Char/Varchar/String field and when you are filtering by a "Stringy" field you must sorround your literals values with single quotes:
SELECT *
FROM wp_before_after
WHERE patientID = 8175
AND patient_display = 1
AND period_taken = '1hour'
Hope this help

Merge 2 tables in MySQL

I would like to merge to tables in MySQL. In SQL I would use the 'MERGE' command, but what is the equivalent command in MySQL? Lets say i have 3 columns in both tables. Then i want to match the rows by the first column, and if there is a match it needs to update 2nd column but keep the original 3rd column and if there isnt a match then it needs to insert the new row.
Here is the SQL code I would like to convert to MySQL.
MERGE [Synsbasen].[dbo].[Koeretoej] AS T
USING [Synsbasen].[dbo].[KoeretoejLoad] AS S ON (T.KoeretoejIdent = S.KoeretoejIdent)
WHEN NOT MATCHED BY TARGET
THEN INSERT(KoeretoejIdent, KoeretoejArtNavn, KoeretoejAnvendelseNavn, RegistreringNummerNummer, KoeretoejOplysningStatus, KoeretoejOplysningFoersteRegistreringDato, KoeretoejOplysningStelNummer, KoeretoejMaerkeTypeNavn, KoeretoejModelTypeNavn, KoeretoejVariantTypeNavn, DrivkraftTypeNavn, SynResultatSynsType, SynResultatSynsDato, SynResultatSynStatusDato, SidsteSynTjek)
VALUES(S.KoeretoejIdent, S.KoeretoejArtNavn, S.KoeretoejAnvendelseNavn, S.RegistreringNummerNummer, S.KoeretoejOplysningStatus, S.KoeretoejOplysningFoersteRegistreringDato, S.KoeretoejOplysningStelNummer, S.KoeretoejMaerkeTypeNavn, S.KoeretoejModelTypeNavn, S.KoeretoejVariantTypeNavn, S.DrivkraftTypeNavn, S.SynResultatSynsType, S.SynResultatSynsDato, S.SynResultatSynStatusDato, CONVERT(VARCHAR(10),'1900-01-01',110))
WHEN MATCHED
THEN UPDATE SET
T.KoeretoejArtNavn = S.KoeretoejArtNavn,
T.KoeretoejAnvendelseNavn = S.KoeretoejAnvendelseNavn,
T.RegistreringNummerNummer = S.RegistreringNummerNummer,
T.KoeretoejOplysningStatus = S.KoeretoejOplysningStatus,
T.KoeretoejOplysningFoersteRegistreringDato = S.KoeretoejOplysningFoersteRegistreringDato,
T.KoeretoejOplysningStelNummer = S.KoeretoejOplysningStelNummer,
T.KoeretoejMaerkeTypeNavn = S.KoeretoejMaerkeTypeNavn,
T.KoeretoejModelTypeNavn = S.KoeretoejModelTypeNavn,
T.KoeretoejVariantTypeNavn = S.KoeretoejVariantTypeNavn,
T.DrivkraftTypeNavn = S.DrivkraftTypeNavn,
T.SynResultatSynsType = S.SynResultatSynsType,
T.SynResultatSynsDato = S.SynResultatSynsDato,
T.SynResultatSynStatusDato = S.SynResultatSynStatusDato;
Look at: https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html
Your query should be something like this:
INSERT into Koeretoej
(KoeretoejIdent, KoeretoejArtNavn, KoeretoejAnvendelseNavn,
RegistreringNummerNummer, KoeretoejOplysningStatus,
KoeretoejOplysningFoersteRegistreringDato, KoeretoejOplysningStelNummer,
KoeretoejMaerkeTypeNavn, KoeretoejModelTypeNavn, KoeretoejVariantTypeNavn,
DrivkraftTypeNavn, SynResultatSynsType, SynResultatSynsDato,
SynResultatSynStatusDato, SidsteSynTjek)
SELECT
S.KoeretoejIdent, S.KoeretoejArtNavn, S.KoeretoejAnvendelseNavn,
S.RegistreringNummerNummer, S.KoeretoejOplysningStatus,
S.KoeretoejOplysningFoersteRegistreringDato,
S.KoeretoejOplysningStelNummer, S.KoeretoejMaerkeTypeNavn,
S.KoeretoejModelTypeNavn, S.KoeretoejVariantTypeNavn,
S.DrivkraftTypeNavn, S.SynResultatSynsType, S.SynResultatSynsDato,
S.SynResultatSynStatusDato, DATE_FORMAT("19000101","%m-%d-%Y")
FROM KoeretoejLoad S LEFT JOIN Koeretoej T ON
T.KoeretoejIdent = S.KoeretoejIdent
ON DUPLICATE KEY UPDATE
KoeretoejArtNavn=S.KoeretoejArtNavn,
KoeretoejAnvendelseNavn=S.KoeretoejAnvendelseNavn,
RegistreringNummerNummer=S.RegistreringNummerNummer,
KoeretoejOplysningStatus=S.KoeretoejOplysningStatus,
KoeretoejOplysningFoersteRegistreringDato=S.KoeretoejOplysningFoersteRegistreringDato,
KoeretoejOplysningStelNummer=S.KoeretoejOplysningStelNummer,
KoeretoejMaerkeTypeNavn=S.KoeretoejMaerkeTypeNavn,
KoeretoejModelTypeNavn=S.KoeretoejModelTypeNavn,
KoeretoejVariantTypeNavn=S.KoeretoejVariantTypeNavn,
DrivkraftTypeNavn=S.DrivkraftTypeNavn,
SynResultatSynsType=S.SynResultatSynsType,
SynResultatSynsDato=S.SynResultatSynsDato,
SynResultatSynStatusDato=S.SynResultatSynStatusDato,
SidsteSynTjek=DATE_FORMAT("19000101","%m-%d-%Y")

MySQL Odd-Joining Issue

I had the following code:
select DB.T1.ID,
DB.T1.B,
DB.T1.C,
DB.T2.ID,
DB.T2.B,
DB.T2.R,
DB.T3.ID,
DB.T3.Q
DB.T1.DUP,
DB.T2.DUP,
DB.T3.DUP
from DB.T1, DB.T2, DB.T3
where DB.T1.id = DB.T2.ID
and DB.T1.id = DB.T3.ID
and DB.T2.id = DB.T3.id
and DB.T1.DUP = 'not_duplicate'
and DB.T2.DUP = 'not_duplicate'
and DB.T3.DUP = 'not_duplicate'
;
The output returned 0 rows, however. So, I changed the values of the "DUP" column in each table from duplicate/not_duplicate instead to 0/1. I tried this code and it worked:
select DB.T1.ID,
DB.T1.B,
DB.T1.C,
DB.T2.ID,
DB.T2.B,
DB.T2.R,
DB.T3.ID,
DB.T3.Q
DB.T1.DUP,
DB.T2.DUP,
DB.T3.DUP
from DB.T1, DB.T2, DB.T3
where DB.T1.id = DB.T2.ID
and DB.T1.id = DB.T3.ID
and DB.T2.id = DB.T3.id
and DB.T1.DUP = 1
and DB.T2.DUP = 1
and DB.T3.DUP = 1
;
The second code works perfectly, the first one returned 0 rows. Does anyone know why was this happening? The values "not_duplicate" and "duplicate" were the exact same strings as the csv's that I imported into the database from. I can't explain why this would be the case and I'm really pretty curious.
Thanks very much!
because the DUP column they dont have this not_duplicate in fields. they have 1
then it doesnt match your query .
the query returns values which are stored in the column DUP .

SQL Server 2008: Error converting data type nvarchar to float

Presently troubleshooting a problem where running this SQL query:
UPDATE tblBenchmarkData
SET OriginalValue = DataValue, OriginalUnitID = DataUnitID,
DataValue = CAST(DataValue AS float) * 1.335
WHERE
FieldDataSetID = '6956beeb-a1e7-47f2-96db-0044746ad6d5'
AND ZEGCodeID IN
(SELECT ZEGCodeID FROM tblZEGCode
WHERE(ZEGCode = 'C004') OR
(LEFT(ZEGParentCode, 4) = 'C004'))
Results in the following error:
Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to float.
The really odd thing is, if I change the UPDATE to SELECT to inspect the values that are retrieved are numerical values:
SELECT DataValue
FROM tblBenchmarkData
WHERE FieldDataSetID = '6956beeb-a1e7-47f2-96db-0044746ad6d5'
AND ZEGCodeID IN
(SELECT ZEGCodeID
FROM tblZEGCode WHERE(ZEGCode = 'C004') OR
(LEFT(ZEGParentCode, 4) = 'C004'))
Here are the results:
DataValue
2285260
1205310
Would like to use TRY_PARSE or something like that; however, we are running on SQL Server 2008 rather than SQL Server 2012. Does anyone have any suggestions? TIA.
It would be helpful to see the schema definition of tblBenchmarkData, but you could try using ISNUMERIC in your query. Something like:
SET DataValue = CASE WHEN ISNUMERIC(DataValue)=1 THEN CAST(DataValue AS float) * 1.335
ELSE 0 END
Order of execution not always matches one's expectations.
If you set a where clause, it generally does not mean the calculations in the select list will only be applied to the rows that match that where. SQL Server may easily decide to do a bulk calculation and then filter out unwanted rows.
That said, you can easily write try_parse yourself:
create function dbo.try_parse(#v nvarchar(30))
returns float
with schemabinding, returns null on null input
as
begin
if isnumeric(#v) = 1
return cast(#v as float);
return null;
end;
So starting with your update query that's giving an error (please forgive me for rewriting it for my own clarity):
UPDATE B
SET
OriginalValue = DataValue,
OriginalUnitID = DataUnitID,
DataValue = CAST(DataValue AS float) * 1.335
FROM
dbo.tblBenchmarkData B
INNER JOIN dbo.tblZEGCode Z
ON B.ZEGCodeID = Z.ZEGCodeID
WHERE
B.FieldDataSetID = '6956beeb-a1e7-47f2-96db-0044746ad6d5'
AND (
Z.ZEGCode = 'C004' OR
Z.ZEGParentCode LIKE 'C004%'
)
I think you'll find that a SELECT statement with exactly the same expressions will give the same error:
SELECT
OriginalValue,
DataValue NewOriginalValue,
OriginalUnitID,
DataUnitID OriginalUnitID,
DataValue,
CAST(DataValue AS float) * 1.335 NewDataValue
FROM
dbo.tblBenchmarkData B
INNER JOIN dbo.tblZEGCode Z
ON B.ZEGCodeID = Z.ZEGCodeID
WHERE
B.FieldDataSetID = '6956beeb-a1e7-47f2-96db-0044746ad6d5'
AND (
Z.ZEGCode = 'C004' OR
Z.ZEGParentCode LIKE 'C004%'
)
This should show you the rows that can't convert:
SELECT
B.*
FROM
dbo.tblBenchmarkData B
INNER JOIN dbo.tblZEGCode Z
ON B.ZEGCodeID = Z.ZEGCodeID
WHERE
B.FieldDataSetID = '6956beeb-a1e7-47f2-96db-0044746ad6d5'
AND (
Z.ZEGCode = 'C004' OR
Z.ZEGParentCode LIKE 'C004%'
)
AND IsNumeric(DataValue) = 0
-- AND IsNumeric(DataValue + 'E0') = 0 -- try this if the prior doesn't work
The trick in the last commented line is to tack on things to the string to force only valid numbers to be numeric. For example, if you wanted only integers, IsNumeric(DataValue + '.0E0') = 0 would show you those that aren't.