Select from database where string = same string with diacritics - mysql

How can I select from database by given string and mysql to return the row on same string with diacritics?
select * from myTable where nume = "Stefan"
and the row from database that should be returned:
id = 1
name = "Ștefan"

You can search like:
SELECT * from myTable
where
CONVERT(nume USING utf8) LIKE '%Stefan%'
But this is a bit unclear, as MySql already know how to search for strings with diactritics.

Related

Is it possible to use a list of items in a SELECT without using a temporary table in MySQL?

I have the following MySQL query:
create temporary table if not exists temp_terms(
term varchar(256) NOT NULL,
soundexterm varchar(4) NOT NULL
);
INSERT INTO temp_terms VALUES ('brocoli', Soundex('brocoli')), ('rice', Soundex('rice')), ('aple', Soundex('aple'));
SELECT tt.term, rt.description, rt.soundex
FROM realtable rt
JOIN ( SELECT term, soundexterm FROM temp_terms ) t on tt.soundexterm = rt.soundex;
drop temporary table if exists temp_terms;
Which returns a result that looks like:
term, description, soundex
brocoli, Broccoli, B624
aple, Apple, A140
brocoli, Bresaola, B624
rice, Rusks, R200
Is there a way to do the same query but using the list of values instead of creating a temporary table and inserting the list values into the table?
The MySQL version I'm querying against is 5.6
I've decided to go with UNION like so:
SELECT 'rice',description, soundex
FROM realtable
WHERE soundex = Soundex('rice')
UNION ALL
SELECT 'aple',description, soundex
FROM realtable
WHERE soundex = Soundex('aple')
UNION ALL
SELECT 'brocoli',description, soundex
FROM realtable
WHERE soundex = Soundex('brocoli');
In my C# code, should anyone care, I just do the following to build the query:
await using var cmd = _connection.CreateCommand();
var sb = new StringBuilder();
for (var i = 0; i < lookupTerms.Count; i++)
{
sb.Append(i == lookupTerms.Count - 1
? $"SELECT #t{i}, description, soundex FROM realtable WHERE soundex = Soundex(#t{i});"
: $"SELECT #t{i}, description, soundex FROM realtable WHERE soundex = Soundex(#t{i}) UNION ");
cmd.Parameters.Add(new MySqlParameter
{
ParameterName = $"#t{i}",
DbType = DbType.String,
Value = lookupTerms[i],
});
}
cmd.CommandText = sb.ToString();

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

how to handle backslashes and quotation mark in spark sql?

there is a json string in the field kv of one spark sql table;
such as
{"cpu_max_freq":"1401000","net_ok":"2","ad_report_status":"1\"}"}
how to select this line by using sql?
I have tried :
select * from table_a where get_json_object(kv, "$.ad_report_status") = '1\\"}'
select * from table_a where get_json_object(kv, "$.ad_report_status") = '1\"}'
select * from table_a where get_json_object(kv, "$.ad_report_status") = '1\\\"}'
select * from table_a where get_json_object(kv, "$.ad_report_status") = '1\\\"}'
none of above sql works !!
so how to match the value of "ad_report_status" field ?

MySQL string selecting commands

How can I select a row specific by string values
SELECT * FROM my_table WHERE name=
-- then some function to test what string it starts with
Its a bit hard to explain so i will explain an example with JavaScript
if(mystring.indexOf('targetstring') != -1){
// if that variable contains this string
}
if(mystring.indexOf('targetstring') == 0){
// if that variable starts with this string
}
So all in all, I want to select rows, when their name(a string column) starts with or contains a specific string.
You can use LIKE and the wildcard %:
SELECT * FROM my_table WHERE name LIKE 'john%'; /* name starts with john*/
SELECT * FROM my_table WHERE name LIKE '%john%'; /* name contains john*/
SELECT * FROM my_table WHERE name like "%string%"

mysql distinct select exclude it self query

how to return string exclude query string it self.
column string = AAA/BBB/CCC
result string = BBB/CCC where column like AAA
column string = AAA/BBB/CCC
result string = CCC where column like AAA/BBB
"SELECT DISTINCT `column` FROM `table` WHERE `column` like '???'";
Thank you.
select distinct replace(column, 'AAA/BBB/', '') as column
from table
where column like 'AAA/BBB/%';