Sorry if the topic name is not reasonable.
I have a column in mySql with value (10,11,12), it's a string, like this
And I have an array to check, how can select this row if array contains any one value 10, 11 or 12. For example:
array(3,4,11) - choose
array(5,6,7) - not choose
array(11,8,10) - choose
Thank you!
Here is the query :
SELECT *
FROM your_table
WHERE FIND_IN_SET(10, custom_categories)
OR FIND_IN_SET(11, custom_categories)
OR FIND_IN_SET(12, custom_categories);
For More Detail : Read
Related
I have a table with BLOB column that some row has BLOB, some empty.
1 Apple BLOB-8KiB
2 Banana
3 Pear BLOB-6KiB
4 Orange BLOB-7KiB
Is there any way I can use PHP MYSQL to get the array like this:
$fruit = array(
array("1",Apple,1),
array("2",Banana,0),
array("3",Pear,1),
array("4",Orange,1)
);
I just want to change the BLOB data with 1, Empty with 0 in my PHP array. Pls help.
Your select statement can use IF and ISNULL (note these are not widely implemented in the same format on different database backends, this is for MySQL).
So you would use:
SELECT ID, Name, IF(ISNULL(BlobField), 0, 1) FROM TableName
IF allows you to choose one of two values according to a logical operation.
ISNULL returns true or false according to whether or not the value is NULL
I have this JSON values in MySQL database
Conditions
1- >
[{"offer_id":"158","offer_start":"2017-12-17 09:21:27","offer_ends":"2017-12-17 10:21:27"},{"offer_id":"167","offer_start":"2017-12-17 12:28:57","offer_ends":"2017-12-17 12:58:57"}]
2 ->
[{"offer_id":"170","offer_start":"2018-01-17 04:26:26","offer_ends":"2018-01-17 05:11:26"},{"offer_id":"167"}]
3 ->
[{"offer_id":"170","offer_start":"2017-12-11 20:49:12","offer_ends":"2017-12-11 20:49:12"}]
3 ->
[{"offer_id":"170"}]
So I need to get the nth offer_ends value like "2017-12-11 20:49:12" using mysql query
Considering that you need to find the data from table1 where the column name offers. You need to fetch data where the "offer_ends" has value 2017-12-11 20:49:12
SELECT JSON_CONTAINS(offers, '2017-12-11 20:49:12', '$.offer_ends')
FROM table1;
Get all the records:
SELECT offers, JSON_EXTRACT(offers, "$.offer_ends")
FROM table1
If you want then you can apply the order by on any column and then get the Top 1 record to get the last record.
Here i have one code which get all the offer_ends value by using MySQL -> SELECT REPLACE(REPLACE(JSON_EXTRACT('[{"offer_id":"23","offer_start":"2017-12-11 20:49:12","offer_ends":"2017-12-11 20:49:12"},{"offer_id":"23","offer_start":"2017-12-11 20:49:12","offer_ends":"2017-12-11 20:49:12"}]', '$[*].offer_ends'),'[',''),']','') as offer_dates
and the result is -> "2017-12-11 20:49:12", "2017-12-11 20:49:12" which shows all offer end dates
So i need to find the nth offer_ends value like this -> "2017-12-11 20:49:12"
Guys i found the solution
SELECT JSON_EXTRACT('[{"offer_id":"158","offer_start":"2018-02-21 13:03:15","offer_ends":"2018-02-21 14:03:15"},{"offer_id":"170","offer_start":"2018-02-21 15:03:15","offer_ends":"2018-02-21 16:03:15"}]',CONCAT("$[",JSON_LENGTH('[{"offer_id":"158","offer_start":"2018-02-21 13:03:15","offer_ends":"2018-02-21 14:03:15"},{"offer_id":"170","offer_start":"2018-02-21 15:03:15","offer_ends":"2018-02-21 16:03:15"}]')-1,"].offer_ends")) as offer
Thank you guys
I need one help. I need to fetch value from table as per multiple ids which are in comma separated string using MySQL. I am explaining my table and query below.
db_basic:
id special name
1 2,3,4,5 Raj
2 4,2,5,6 Rahul
3 3,5,6 Rocky
My code is given below.
$special=2;
$qry=mysqli_query($connect,"select * from db_basic where special='".$special."'");
Here I need where special=2 is present inside that comma separated string those value will be fetched. I need only proper query. Please help me.
use below query
May be work for u
Use LIKE instead of "="
"select * from db_basic where special LIKE '%".$special."%'"
use FIND_IN_SET function for ex.
select * from db_basic where FIND_IN_SET(2,special)
Try this once,
select * from db_basic where FIND_IN_SET($special, special);
It should work.
if you current code expeted to return the first and second line of your db, you can use the LIKE operator in the query like this :
$special=2;
$qry=mysqli_query($connect,"select * from db_basic where special LIKE '%,".$special.",%'");
or LOCATE like :
$special=2;
$qry=mysqli_query($connect,"select * from db_basic where LOCATE(".$special.",special)>0");
And FIND_IN_SET : From the docco here - "This function does not work properly if the first argument contains a comma (“,”) character".
Any quick assistance will be highly appreciated. I've table row in mysql named as mob_categories, now data is being is saved as '|' separated for instance (cat 1|cat 2|cat 3 and so on. Now i need to get the value from another column in the same table if the input value matches.
for instance if the value is cat 1 i need to select the value from another column is named as deveice_token wherever it matches with 'cat 1'
I tried this code but its not working somehow
SELECT * from table_name where find_in_set('cat 1',mob_categories) <> 0
so i modified a bit with the following code but it works with only numeric value if the value exits in this format in this format (i.e) 1,2,3
$userNotification = $wpdb->get_results("SELECT * from table_name where 1 IN (mob_categories); ");
What's i'm missing specifically?
Since data in mob_categories are separated by | rather than by comma (,) so you need to make it compatible for FIND_IN_SET first.
So replace all the | by comma (,) first.
SELECT * from table_name where find_in_set('cat 1', REPLACE(mob_categories,'|',',')) > 0
I am want to write a php mysql query which includes and and range condition.
In the screen shot you can see the field of the table called search. The fields with same name are the range. I want select query and it should include all the fields and their appropriate range.
The names of the fields are shape1,shape2(it is range from shape1 to shape2) etc and it goes on.
The query should be like this
select * from search where unique_id='$unique_id && (carat1='$carat1' between carat2='carat2') &&...
and there are other field too like cut and shape, all in one query. I am inserting value in to the database directly from android in json format. My problem is that i don't know proper format.
Please help me
It should go like this (an example)
select * from search
where unique_id= 10
and carat1 between 1 and 10
and shape1 between 1 and 10
and cut1 between 1 and 10
EDIT:
If you are trying to run the SQL from PHP script then the format will be different like below (if the column is type integer like unique_id then don't put ' while replacing value. For string type replace with ' like carat)
Select * from search
where unique_id = $unique_id
and carat between '$carat1' and '$carat2'
and color between '$color1' and '$color2'
and shape between '$shape1' and '$shape2'
Example usage of between in mysql:
SELECT * FROM search
WHERE unique_id = 200 AND
carat BETWEEN 1 AND 5;
Note that the AND that follows BETWEEN is used to indicate the range (1,5) and is not a logical AND operator.
Try this, I hope this will help you
SELECT * FROM search
WHERE unique_id = $unique_id AND
carat between $carat1 AND $carat2 AND
color like '%".$color1."%' AND
color like '%".$color2."%' AND
shape like '%".$shape1."%' AND
shape like '%".$shape2."%'