MySql select values of serialized column with PHP [duplicate] - php

How do i search from serialize field in mysql database except mysql like statement?
The data is:
a:9:{s:2:"m1";s:4:"1217";s:2:"m2";s:8:"9986-961";s:2:"m3";s:19:"1988-03-07 00:00:00";s:2:"m4";s:0:"";s:2:"m5";s:0:"";s:2:"m6";s:0:"";s:2:"m7";s:3:"104";s:2:"m8";s:6:"150000";s :2:"m9";s:18:"Ok Then, Yes It Is";}
I need the row in which the m9 value is 'Yes It Is'. I do not want to use mysql 'like' statement.
I have tried:
SELECT * FROM table WHERE field like '%Yes It Is%'
Can you please help.

Have you tried the following:
SELECT * FROM table WHERE field like '%"m9";s:18:"Ok Then, Yes It Is";%'
?
But in fact if you want to search in such data, you should simple create proper structure of your table and not to put all serialized data in one column

Related

How to do make Where clues to compare input data with Columns

I want to get DATA from mysql TABLE by submit this multiple select value & compare with this columns.
http://prntscr.com/nmsmw1
http://prntscr.com/nmsnts
I followed this , but not working.
$Data=Employee::whereIn('skills', $request->skills)->get();
you can convert this sql query into laravel, So you can use FIND_IN_SET() in Laravel like as bellow query.
$data = Employee::whereRaw("find_in_set('".$request->skills."',employees.skills)")->get();

where condition for json_encode value stored in database

My value stored in the database looks like this ["7","8"] and i want to check if my search value is present in it for that i had used my code like this
$this->db->where_in(json_decode('tool_id'),$tool_id);
$this->db->where('status',0);
$query=$this->db->get('tbl_tools_supplied');
return $query->result();
my all values are stored like this in tool_id column here its not getting the output so is there any other way
This works for me
SELECT * FROM table_name WHERE FIND_IN_SET("7", REPLACE(REPLACE(tool_id,'[', ''),
']',''))
you can not search your id in where condition of sql query if column store data as json,but you can fetch all records and then filter out result array of mysql query by match your id.

How to get column names from a query in IBM db2 via SQL

Is it possible to get the list column names from a query in IBM db2?
Consider a very simple example I want the column names fetched by following SQL;
select * from db.t1,db.t2 where t1.id = t2.id
Actually I know how to get column names from a a single table. But facing difficulties to get column names from such scenario.
I want the list of columns as an Array in PHP. It can be done if I just add "FETCH FIRST 1 ROW ONLY" as the end of the SQL, and run it. Then from result set I can get the columns.
But if there is no data then also I need the list of columns. How to achieve that?
Any help would be great for me.
You can use db2_num_fields() to determine the number of columns in the result set, then loop over them and call db2_field_name() to obtain the names.
You could always just do something like
describe select * from tablea, tableb

Selecting a serialized value from MySQL with a REGEX

I have a serialized field called details in a MySQL DB that looks like this:
a:4:{s:9:"bootstrap";s:14:"Boostrap_3.3.x";s:7:"layouts";s:10:"Responsive";s:13:"preprocessors";s:0:"";s:8:"browsers";s:117:"Latest_Chrome,Latest_Firefox,Latest_Safari,Internet_Explorer_11,Internet_Explorer_10,Internet_Explorer_9,Latest_Opera";}
I'm trying to retrieve records with a value containing Bootstrap_3.x
I've been able to retrieve the details field data based on the key: bootstrap with the following:
SELECT details as bootstrap_version FROM product_info WHERE details LIKE '%bootstrap%'
This query returns all records that have a field called bootstrap.
What do I need to change in order to select values that contain Bootstrap_3.x?
Any help would be appreciated.
hanks,
-Paul
In MySQL you can use regular expressions in your queries:
SELECT details as bootstrap_version FROM product_info
WHERE details REGEXP 'bootstrap";s:\\d+:"Bootstrap_3\.x'
However, if this information is something that you plan on using for search more often, I would suggest putting it in another column that you can index - regexp in queries are very slow.
Read more in the documentation: https://dev.mysql.com/doc/refman/5.7/en/regexp.html
You should user REGEXP:
SELECT details AS bootstrap_version FROM product_info WHERE details REGEXP 'Boostrap_3(\\.[0-9]+)+'

How do you add all the values of a particular column from mysql DB using php?

Im pulling _balance from a DB and then want to add all the values in the column.. how do I accomplish this with PHP?
SELECT _balance FROM A WHERE X='X' and Y='Y' then add all the values of _balance... ?
Why not do it from within the DB
SELECT SUM(_balance) FROM A WHERE .... // whatever your where clause needs to be

Categories