I'm trying to fetch the data in NodeJs using mysql IN query.
I'm getting the data dynamically and need to get data based on textValues;
For example , let textValues = 'a1b, a2b, a3b, a4b' and my query is-
select * from table where values IN (textValues).
but this will not generate the expected output.
As I'll achieve this using
select * from table where values IN ('a1b','a2b','a3b','a4b').
i.e. textValues should be in 'a1b','a2b' which we can't store in any data types like this.
How can we achieve this with MYSQL IN query.
Thank You
Related
I have a table name data with a column name body containing records present in json format i.e.,
'{"createdOn": "2018-10-05T00:00:00.000+0000","id":1,"name":"abc"}',
'{"createdOn": "2018-10-06T00:00:00.000+0000","id":2,"name":"xyz"}',
'{"createdOn": "2018-10-10T00:00:00.000+0000","id":3,"name":"aaa"}',
'{"createdOn": "2018-10-25T00:00:00.000+0000","id":4,"name":"qqq"}',
I am looking to fetch and delete the records between "createdOn":"2018-10-05T00:00:00.000+0000" and "createdOn":"2018-10-10T00:00:00.000+0000" for which I am trying to write a query SELECT * FROM data where body like between '%"createdOn": "2018-10-05%' and '%"createdOn": "2018-10-10%'; but it is giving the error . How can something like this be achieved ? If anybody can help ?
Thanks
Yes, you can extract values from json and use them in queries:
select * from data where JSON_VALUE(body, '$.createdOn') between '2018-10-05T00:00:00.000+0000' and '2018-10-10T00:00:00.000+0000';
select * from data where JSON_VALUE(body, '$.name') LIKE '%a%';
I am trying to combine two rows of generated json within mysql, my current code outputs two rows (first is user id, second is the time) I want to combine them into one string. I've explored the use of GROUP CONCAT, JSON_MERGE (and JSON_MERGE_PRESERVE) and also JSON_OBJECTAGG. For further info, I am using MYSQL8.
Current output:
unavailability
{"59745190": "1400"}
{"59745190": "1200"}
My MySQL Script:
SELECT JSON_OBJECT(`appointment_with`, TIME_FORMAT(`appointment_datetime`,'%H%i')) as `unavailability`
FROM `appointments`
WHERE `appointment_with` = '59745190'
AND (DATE(`appointment_datetime`) = '2022-03-30' AND `appointment_confirmed` = 1)
Thank you in advance.
I've created a MySQL sproc which returns 3 separate result sets. I'm implementing the npm mysql package downstream to exec the sproc and get a result structured in json with the 3 result sets. I need the ability to filter the json result sets that are returned based on some type of indicator in each result set. For example, if I wanted to get the result set from the json response which deals specifically with Suppliers then I could use some type of js filter similar to this:
var supplierResultSet = mySqlJsonResults.filter(x => x.ResultType === 'SupplierResults');
I think SQL Server provides the ability to include a hard-coded column value in a SQL result set like this:
select
'SupplierResults',
*
from
supplier
However, this approach appears to be invalid in MySQL b/c MySQL Workbench is telling me that the sproc syntax is invalid and won't let me save the changes. Do you know if something like what I'm trying to achieve is possible in MySQL and if not then can you recommend alternative approaches that would help me achieve my ultimate goal of including some type of fixed indicator in each result set to provide a handle for downstream filtering of the json response?
If I followed you correctly, you just need to prefix * with the table name or alias:
select 'SupplierResults' hardcoded, s.* from supplier s
As far as I know, this is the SQL Standard. select * is valid only when no other expression is added in the selec clause; SQL Server is lax about this, but most other databases follow the standard.
It is also a good idea to assign a name to the column that contains the hardcoded value (I named it hardcoded in the above query).
In MySQL you can simply put the * first:
SELECT *, 'SupplierResults'
FROM supplier
Demo on dbfiddle
To be more specific, in your case, in your query you would need to do this
select
'SupplierResults',
supplier.* -- <-- this
from
supplier
Try this
create table a (f1 int);
insert into a values (1);
select 'xxx', f1, a.* from a;
Basically, if there are other fields in select, prefix '*' with table name or alias
Here is JSON data in a database column named "debts":
{
"debt_block_1":{
"debt_type_1":"House",
"repayment_frequency_1":"Monthly",
"financial_institution_1":"anz",
"repayment_amount_1":"899",
"amount_your_share_1":"98",
"loan_term_1":"3",
"rate_1":"3"
},
"debt_block_2":{
"debt_type_2":"Creditcard",
"repayment_frequency_2":"Monthly",
"financial_institution_2":"anz",
"credit_limit_2":"2000",
"remaining_balance_2":"200"
},
"special_notes":""
}
Here, I need to get debt types if financial institution is "anz". I have no experience with writing SQL queries for JSON data types.
I tried the following query:
select JSON_SEARCH(debts, 'all' , 'anz') from table_name;
and the result of the query is given below:
["$.debt_block_1.financial_institution_1", "$.debt...
Editing start
Another query I executed is
SELECT debts, JSON_EXTRACT(debts, "$.debt_type")
FROM application_data
WHERE JSON_EXTRACT(debts, "$.financial_institution") = 'anz'
No result.
Editing end
Any suggestions on how I could get the debt_type value where financial_institution ="anz"?
You would have an easier time finding the desired records if you were able to use JSON_CONATINS looking for 'finanical_instituion'. But having 'financial_instituion_1', 'financial_instituiton_2' means you have to search for all of those variants.
How to write or create command as
select * from profile where name like r% limit 12,16;
in mysql to get the appropriate result in yii.
please any help would be appreciated. thank you in advance
You should use this code:
$queryResult = Yii::app()->db->createCommand('select * from profile where name like "r%" limit 12,16;')->queryAll();
Result will be an array of associative arrays where keys will represents column names, and values are values fetched from DB.