how to handle backslashes and quotation mark in spark sql? - mysql

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 ?

Related

How can i set a string values based on integer variable while executing sql query?

SQL Query
sessionFactory.getCurrentSession().createSQLQuery("select claim.encounterId, claim.claimUniqID, patientmaster.FirstName, tbl_insurance.insurance_name, claim.status from rcmdb.claim join rcmdb.encounter on claim.encounterID=encounter.encounterID join rcmdb.insurance_details on encounter.insuranceDetailsID=insurance_details.insuranceDetailsID
join rcmdb.tbl_insurance on insurance_details.insurance=tbl_insurance.insurance_id
join rcmdb.patientmaster onpatientmaster.patientMasterID=encounter.patientMasterID
where createdByDate between'"+from+"' and '"+to+"'").list();
i want to return string values based on claim.status values like if the status is 1 accepted, in output I want the string values how can I write the query?
You can use CASE statement. https://www.w3schools.com/sql/func_mysql_case.asp
SELECT CASE
WHEN status =1 THEN STRING
ELSE NULL
END
Put a table in the database that maps the int to the string and join it:
ClaimStatus
--------------
ID, StatusDescription
1, Accepted
2, Rejected
SELECT c.PolicyNumber, c.ClaimValue, cs.StatusDescription
FROM
claims c
INNER JOIN claimstatus cs ON c.ClaimStatusId = cs.ID

Convert SQL query to SQLAlchemy

I would like to convert my query into SQLalchemy:
SELECT sel.ID,sel.URL,GROUP_CONCAT(sel.label_values)
FROM
(
SELECT table1.id as ID, table1.url as URL,CONCAT_WS(':',label
,value) as label_values
FROM table1 LEFT OUTER JOIN table2 ON table1.id =
table2.media_id
WHERE table1.type = "image" AND table1.id >= 0
ORDER BY table1.id
) AS sel
GROUP BY sel.ID
I tried something like this:
label_and_values_column = func.CONCAT_WS(' : ',table2.c.label,
table2.c.value).label(name='label_and_values_column')
outer_join_select = select([table1, label_and_values_column])\
.order_by(table1.c.id)\
.select_from(table1.outerjoin(table2))\
.label(name='outer_join_select')
all_ = func.group_concat(' : ',outer_join_select.id)\
.label(name='all_label_and_values_per_media_id')
The last line gives me
Caught exception: Neither 'Label' object nor 'Comparator' object has an attribute 'id'.
I am using db connection with transaction with engine.begin() as db_conn_transaction:
meta = sqlalchemy.MetaData(db_conn_transaction)
I would like to construct select_statment and then use select_statment.execute().fetchall()
Can't use .subquery() since I don't have session.query()...
Any help for converting my query in SQLalchemy is welcomed!

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

Select from database where string = same string with diacritics

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.

MySQL- How to select rows which matches any value from an array

How to select rows from a table where its condition can match any value from an array.
something like this:
Select * from Table Where Name = Array_of_Names;
Array_of_Names is a java array.
You can pass it using IN keyword in query with multiple items separated by comma in brackets like :
String query = "Select * from Table Where Name IN (";
for(int i =0 ;i<arrayName.length();i++){
query = query + "'" +arrayName(i) + "'" + ",";
}
query = query.substring(0, query.length()-1);
query = query + ")";
// execute your query here
This ll pass your query like :
Select * from Table Where Name IN ('arrayvalue1','arrayvalue2','arrayvalue3');
as per length of array.
You'll need to craft the SQL statement and use WHERE ... IN ...
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);
here you are:
Select * from Table Where Name in ("Tom", "Dick", "Harry");