How to match Value from string in Mysql PHP - php

I have a table methodology in mysql where a column contain values like 1,2,3,4,5,6 or 3,4,6 or 25,4,7,8 in multiple row.
Now in my scenario i have a company which contain id 6 and i want to Match this value with methodology table value. But i don't have any idea how i can do it.
I am trying that first it get all values from methodology table one by one and after that match value of company.
can anyone please help me??

You can use FIND_IN_SET(your_id, columnname) function.
Query will be like
SELECT * FROM
methodology
where FIND_IN_SET(your_id, columnname)
For more details about function please refer : http://www.w3resource.com/mysql/string-functions/mysql-find_in_set-function.php

Use LIKE from mysql queries.
An example :
SELECT row FROM my_table WHERE my_column LIKE %6%

You need to use find_in_set function
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set

To elaborate on #matthiasbe's incomplete answer, if you want to use LIKE, you should do as follows :
SELECT * FROM methodology WHERE companies LIKE '%,6,%' OR companies LIKE '6,%' OR companies LIKE '%,6'
This will match all rows where companies either has ,6, in its value or begins with 6, or ends with ,6.
This is far from being a perfect solution (both in clarity of the code and execution time), I'm just listing the options, hope it helps.

Related

How to separate SQL column data separated by comma to individual values and then count these values

Am using a SQL command in PHP to count the no of values inserted in a column named attack_type. I want to count the occurrence of individual values like website defacement in the whole column of the table. But here the column attack_type contain different values, separated by a comma and the count is treating whole column data as a string. Below is my current SQL statement with its output
I tried explode print_r in PHP
SELECT attack_type,
count(*) as number
FROM data_input_test
GROUP BY attack_type
Here is the output of the above statement
generated:
https://drive.google.com/open?id=1TyRL_Mh0OOJWaCpFczxmBr34No9LUpzH
But what I want is :
https://drive.google.com/open?id=1eeA_1TCER0WMpZwSkBDMzRtRa8xihbZd
and so on. The above desired output is edited to show what I exactly want.
Other answer on stackoverflow and on other forums are either irrelevant or are using regrex or a new table creation in one or the other way. That I don't want as my hosting has some limitations. My hosting doesnt provide creation of triggers, regrex or creation of temp tables
I may have a solution for this but don't know how to apply here. Possible here: https://www.periscopedata.com/blog/splitting-comma-separated-values-in-mysql
Please someone explain me how to apply the same here.
So I finally worked around to get my work done using the select only. This only works if you have a finite set of data or specifically less than 64 values.
Change your column datatype to 'set' type. And enter your set values.
Now use select, count, find_in_set and union functions of sql.
Example:
union select 'Un-patched Vulnerable Software Exploitaion'as type, count(*) as number from data_input_test where find_in_set('Un-patched Vulnerable Software Exploitaion',attack_type)```
and so on for all your values
I know this is not how you should do but as the legends say this works 😎😎
If you just want to count comma-separated values in rows, you can use:
SELECT SUM(LENGTH(attack_type) - LENGTH(replace(attack_type, ',', '')) +1) AS TotalCount
FROM table_name;

sql statement syntax for search

I would like to write an sql statement for search. Here is sample database structure.
For eg, I want to display all records with topic '13'. How can i write sql query for searching 13 from the above structure? Any suggestions?
Can i able to use WHERE Topic LIKE '%13%'? Anything wrong with this?
Try this one:
SELECT * FROM `TABLE_NAME` WHERE `Topic` LIKE "%13%";
It's better and faster to save it in a third table of many-to-many relationship.
If you want to save as per your example (single table), try to save data as eg ",10,13,15,"
always have coma before and after, thus the following sql will exclude 213 and 132 etc
select * from table_name where Topic like '%,13,%'
select * from table where find_in_set("13",topic);
or if topic is not used as a set, you could do ...
select * from table where concat(",",topic) like "%,13,%";
The 2nd isn't real elegant but I've had to do that a couple times.
Because the data isn't really normalized, I used concat to add a comma to the topic field so I could make sure the "like" comparison would pass with a comma before and after the value. I suppose we would also have to remove any unwanted spaces as well for this example, so ultimately it would end up like:
select * from TABLE where concat(",",replace(topic," ","")) like "%,13,%";
Ultimately, we have to know what to expect in the topic column to come up with a query that would always work. In the past, I've had situations where I would add values to a string field (i.e. topic) with a delimiter before and after each value like:
(1)(2)(3)(14)(15)(255)(283)
If you did something like this for the topic field, the query is simple ...
select * from table where topic like "%(13)%";

php search a mysql string results

guys just a quick question. I have a string in a mysql row resembling this
'google.co.nz, stackoverflow.com, facebook.com, grubber.co.nz'
and so on... I would like to search all the rows in the table and check how many times 'facebook.com' shows up in all the rows that are like the ones above so to clarify
my rows look like this
-- id -- user -- likes
1 bob facebook.com, google.co.nz, grubber.co.nz, stackoverflow.com
and i would like to check how many times facebook.com shows up in the whole table (in every row)
Assuming every user can only like the same page once, this should work:
SELECT COUNT(*)
FROM table
WHERE likes REGEXP '[[:<:]]facebook.com[[:>:]]'
PS: Normalize your table, you will run into serious trouble with a layout like this in the very near future!
You could do it in a lazy fashion, issuing a LIKE SQL query :
SELECT count(*) FROM my_table
WHERE my_table.likes LIKE '%facebook.com%'
This is really (REALLY) not cpu friendly. Especially with large tables
Otherwise, you could use MySQL fulltext indexes feature.
You may find more details in this article
you could use the "LIKE" statement.
It goes like this (if you want to know which user likes facebook.com)
SELECT * FROM yourtable
WHERE likes LIKE '%facebook.com%'
Anyway, please consider storing the likes in a 1:n table construct and not as a string
By that you have a a table storing all like-able sites, a user table and a user-likes table storing the assignment.
Other option. Get the row data in a variable and then use the function preg_match_all (http://www.php.net/manual/es/function.preg-match-all.php).
This function returns the number of occurrences of the regular expression passed in the first parameter.
Try this
select count(likes) from yourTable where likes like '%facebook.com%'
You can use the like operator to match the pattern:
SELECT COUNT(id) AS occurences WHERE likes LIKE "%facebook.com%"

Sorting in a sql query, disregarding the numbers at the start of the field?

I need to sort items on a website in a bit of a weird way:
Just say my data is:
681A
500AB
300BB
i need it to be in this order, so i need it to disregard the number at the start and order by the first letter it encounters.
Is there a way to do this in a sql query? Or will i have to create an array on the page and sort it that way with php?
Thanks in advance if anyone can help out.
A way you can try to do that is to add a substring in your query and use it to sort.
Note that this will work only if you always have 3 char to ignore.
SELECT fieldA, fieldB, SUBSTRING(fieldB,4) as subfieldB FROM myTable order by subsfieldB
You have to create additional column in your table, storing these abbreviations in the form you'd like to be sorted.
If the prefix number is always 3 digits long, then this would be reasonable:
SELECT SUBSTR(field1,4) AS shortfield1
FROM table
ORDER BY shortcol
see
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substr
Take a look how to make a regex replacement on sql or try to use replace function some thing like this:
SELECT * FROM (
SELECT REPLACE([YourCol],'0','') AS COL, *
FROM [table]
) as T
ORDER BY [COL]
note that regex does not has a great performance
select * from table order by substring(NNNN,startposition,endposition) asc

MySQL - Filter result

I want to filter result of my SQL query. I want to select everything that has some specific text in some column.
Example:
SELECT * FROM categories WHERE (name
has 'abc' values in it's value ex.
MyabcCategory)
Also maybe it is not very good idea to do that in query, maybe it is better to get all and then filter array instead? But I don't know how to do that aether.
Use LIKE with % wildcard:
SELECT * FROM categories WHERE name LIKE '%abc%'
This will give you all the records that have abc somewhere in them.
You can learn more about it here :)
You want to use the LIKE operator, with % to match any character before and after your specific word :
select *
from categories
where name like '%abc%';
But note that doing so, MySQL will scan each line of the table, every time the query is executed... which might not be great if you have a lot of data.
If you're searching for some kind of text, you might either want to :
Use a FULLTEXT index, if you're working with MyISAM tables.
Or, use a solution that's separated from MySQL, with a specific indexing/search engine, such as Solr.
SELECT * FROM categories WHERE name LIKE '%abc%'

Categories