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%"
Related
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.
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)%";
I am having a table with a column that has few ids that were put into database with multi select. Column for example contains: 1,4,5,7,9. Is it possible to check if this column contains for example number 5 or not in it through MySQL query ?.
I need to select all the people that have number 5 or some other listed in that field and print them through php.
http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_find-in-set
SELECT ...
WHERE FIND_IN_SET(5, list_column)
But understand that this search is bound to be very slow. It cannot use an index, and it will cause a full table-scan (reading every row in the table). As the table grows, the query will become unusably slow.
Please read my answer to Is storing a delimited list in a database column really that bad?
You can use #MikeChristensen's answer to be more standard. Another trick with standard SQL is this:
select * from TableName
where ',' || ids || ',' LIKE '%,5,%'
(in standard SQL, || is the string concatenation operator, but in MySQL, you have to SET SQL_MODE=PIPES_AS_CONCAT or SET SQL_MODE=ANSI to get that behavior.)
Another MySQL-specific solution is to use a special word-boundary regular expression, which will match either the comma punctuation or beginning/end of string:
select * from TableName
where ids RLIKE '[[:<:]]5[[:>:]]'
None of these solutions scale well; they all cause table-scans. Sorry I understand you cannot change the database design, but if your project next requires to make the query faster, you can tell them it's not possible without redesigning the table.
Perhaps:
select * from TableName
where ids = '5' -- only 5
or ids like '5,%' -- begins with 5
or ids like '%,5' -- ends with 5
or ids like '%,5,%' -- 5 in the middle somewhere
It probably won't be very fast on large amounts of data. I'd suggest normalizing these multi-selection values into a new table, where each selection is a single row with a link to TableName.
select * from your_table where concat(',',target_column,',') like '%,5,%'
you can write the sql query like this, for example you are looking for the number 5
select * from your_table_name where ids='5'
if you want to check the result with php just tell me i will write it for you :)
if i select these particular rows using
select * from wp_posts where title like 'abc%'
it displays the rows
now i want to delete these rows using php
Can i do this or iam wrong
thanks in advance
It would be more interesting if you inform your code a little more complete. But basically the SQL command is as follows:
delete from wp_posts where title like 'abc%'
If there is sensitive data like activation keys or something, you need to use binary function of mysql to check whether abc is matching with ABC or not.
Use this one:
delete from wp_posts where title like binary('ABC%')
This is a bit of a difficult problem for me to word, and I may be going about it in the completely wrong way.
I'm storing a set of options in a database, where each option is its own column. The user can change the number of options, however, so I need a way of allowing PHP to always select all the options.
Let's say I have these columns: options_dialog_1, options_dialog_2, options_dialog_3, options_dialog_4
There could be a varying number of these dialog option columns, eg, another called options_dialog_5 could be added.
How do I select all the dialog option columns, based on their column name format?
I think you have a database design problem here; repeating columns like that always leads to trouble in the end. I think you need two tables, one for the user and one for the options defined something like this...
USERS
id
name
OPTIONS
id
user_id
option_dialogue_number
option_dialogue_value
That turns the columns into rows, which are rather easier to get at.
Brian's answer will really, really pay you off in longer period. But if you need something quick & ugly, you can check out the "metadata dictionary" (tables that store information about all other tables, columns etc). You could get list of columns from it with first query and use it to build the second one.
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='mytable' AND COLUMN_NAME LIKE 'options_dialog%'
Visit the manual on INFORMATION_SCHEMA for more goodies.
I am not sure I understand the problem. Are you looking for
SELECT * FROM options_table
Something like (faux SQL - wont work)
SELECT ( SELECT column_names where column_name LIKE 'options_dialog%' )
FROM options_table
sounds not feasible to me (though I am sure it's possible somehow). If you need this, either consider refactoring the database design or maybe use a bitmask to store the selected options in a single column.