i have a question about searching in MySQL.I couldn't find answer for long time.
I use symfony 3.1 and i have the next situation:
I have column site_languages (longtext, (DC2Type:simple_array) ) in sql with values (for example) "0,1,7", "11,15,27" etc
So my question is:
how i can select row by site_languages which include in array a search value?
I tried use LIKE, checked REGEXP but for example if i need search by value "1" it will return rows with 11/51/111 etc too.I was thinking about keep values like "[1],[15]" But i think there is exist easier and right solution for it?
If you'd like to solve this issue by using a regex you can use this regex (^|,)1(,|$) to match a 1 that is preceded by a comma and followed by a comma OR the 1 may be preceded by the beginning of the line or end of the line.
You should consider normalizing your DB table but if you insist to keep it this way you can save it as json then retrieve all rows and iterate (bad idea) but is ok if it is a short table
Related
I'm using implode to insert few values into one row in MySQL database.
implode(' ', $_POST['tag']);
Assuming that I have table named product with row named tags with 3 different values that inserted inside like this:
usb adapter charger
I have tried using this method using like operator (%), but that didn't worked.
$sql = "SELECT * FROM product WHERE tags='%usb%'";
How can I extract only one value from the imploded array using WHERE in mysql query?
I agree with the comments about re-designing the database. At first read it seems that using LIKE would definitely get the result you want but after reading #Patrick Q's pan - panther example, it makes a lot sense that LIKE is not really a good solution. There are ways to get exactly the tag string you're looking for but it may hurt the performance and the query will be longer and complex. Hence the following are to demonstrate how the query would look like with your current tags data value:
MySQL query:
SELECT tags,
SUBSTRING_INDEX(SUBSTRING_INDEX(tags,' ',FIND_IN_SET('usb',REPLACE(tags,' ',','))),' ',-1) v
FROM mytable
HAVING v = 'usb';
As you can see, there are a few functions being used just to get the exact string from the data cell. Since your example data was separating with spaces and FIND_IN_SET identify value separation by comma, REPLACE take place on the tags column first to replace spaces with comma. Then with SUBSTRING_INDEX twice to get the string using the location extracted in FIND_IN_SET. Finally at the end HAVING to get only the tag you're looking for.
Further demo here : https://www.db-fiddle.com/f/joDa7MNcQL2RakTgBa7qBM/3
I'm using PHP for a school project and I'm trying to run an SQL query that will return a list of entries in a table where they all contain 8 digits of a 12 digit ID. E.g. I have a string containing "0221122017" and I want to query this to return a list of values that resemble "0221122017XXXX". Is there an easy way to do this? I have not been able to find that carries out this sort of function for an SQL Query. I have no problem with the PHP side and running a query etc. I am really just racking my brains as to how this would be done. I hope I have explained my problem well
Thanks
You can use LIKE
SELECT *
FROM yourTable
WHERE id LIKE '%0221122017%'
LIKE is followed by a pattern string, and % is a wildcard that matches anything.
I have a new question cause i didnt find it anywhere.
I have a db which contains 4 columns. I did my bot to insert array to a column.Now i have to fill another columns.
My filled column contains site links. Exmp: www.dizipub.com/person-of-interest-1-sezon-2-bolum-izle
I need to take "person-of-ınterest" part and write it to another column as kind of a "Person of Interest". And also "1-sezon-2-bolum" as "Sezon 1 - Bölüm 1".
I couldnt find it to do with php not sql. I need to make it with bot. Can someone help me about it please.
database
There is a column named bolumlink where i put the links. As i told i need to take some words from these links. For instance:
dizi column needs to be filled with "Pretty Little Liars" in first 9 row.
It can be done by SQL Update with Like which allows you to select rows with pattern based search using wild-cards:
% matches any number of characters, even zero characters.
_ matches exactly one character.
update your_table set dizi = 'Pretty Little Liars' where bolumlink like '%pretty-little-liars%'
NOTE:
Updating your database using like without limit or conditions with unique columns can be dangerous. This code might affect the whole table if empty string is passed.
I have a table in MySql with several different fields, one of them contains a description that could be a couple of paragraphs long.
I am trying to figure out a way to have php automatically go through these description fields and create a list of the top keywords used. I am looking for the top keywords for the entire table, not each post individually.
I know this is a bit of a resource heavy operation, and it wouldn't be run very often anyways.
But I'd like to get a list like this:
some x 121
most x 110
frequent x 90
words x 50
So that I could see what the top used words are in the description field. Any idea at all where to start?
You can run you query,
loop through the records and append descriptions together into 1 big happy string.
Then, you can explode by ' ' into array
Get array of values using array_count_values()
Re-sort in descending order arsort()
Update
Sample code too:
$string = '';
foreach (your_result_set as one_row)
{
$string .= $one_row['text'];
}
$data = explode(' ', $string);
$data = array_count_values($data);
arsort($data);
If you have control over the database one way would be to add triggers to this table that maintains another table with all keywords.
The insert trigger would go through new.description and increment all keywords found
The delete trigger would do the same but for old.description and decrement the keywords
The update trigger would do the same as delete and insert, ie decrease all found in old.description and increase for new.description.
Once you have done and tried these triggers dump all data and re import it to have the trigger do the work on all existing data.
there are a few ways you can do this. i'm guessing you dont want to count every word, as words like and,if,it etc will all be meaningless.
also how many rows are we talking?
a simple solution is to create and array called words. loop through each row.
explode the paragraph using " ", which gives you each word. you may also wish to do a str_to_lower first if case is an issue.
loop through and use array_key_exists to see if there is a key if not create it.
and add a value of one. otherwise incriment the value by one.
this will give you counts of each word.
if this is for a search of a large database it would be worthwhile adding keywords to a seperate table on insert.
one way i think this would be good is to add the 5 most frequent used words excluding those in the exclude list (and,it,or,a,i etc). and add any word that appears in the keyword table.
there are issues with this. i have this response and dont mention php, sql or query which are what the post related to .maybe it would be worth having tags/keywords added on insert.
Regarding this line
database::query("DELETE FROM bo WHERE name='{$this->_protected_arr[a1]}' AND email='$_SESSION[email]'");
How can I update it so that it only deletes one row instead of all of them.
Also, the syntax looks off, is there any way not to use the {}.
Also, normally PHP calls warnings if ther are no apostrohpes in associative array but it does not pick it up in this case. So this seems wrong as well.
So this is actually 3 questions.
How can I update it so that it only deletes one row instead of all of them.
Use a LIMIT 1 clause at the end of the query.
the syntax looks off
It's correct. It's what PHP calls the "complex (curly) syntax" for variable parsing in strings:
http://php.net/manual/en/language.types.string.php#language.types.string.parsing.complex
You can of course use string concatenation there if you prefer.
You should use LIMIT 1. The question is which row will be caught in LIMIT 1 selection :)
The best case scenario is that your database::query supports placeholders.
Concerning curly brakets, just add apostrophes to the array key
database::query("DELETE FROM bo WHERE name='{$this->_protected_arr['a1']}' AND email='{$_SESSION['email']}'");
If you don't wanna use curly brakets, then just concatenate strings like "DELETE ...".$this->property." AND ...".
database::query("DELETE FROM bo WHERE name='{$this->_protected_arr[a1]}' AND email='$_SESSION[email]' LIMIT 1");
As everyone else suggested, adding a LIMIT 1 to the end of your query will make it so that only one row is deleted. However, given you're using static classes here, I'm guessing you know enough to understand why everything in a database should have its own unique identifier and I'm curious why you aren't using that as part of your criteria for determining what you delete? Or there should at least be some other comparison you can do, like running a select statement that pulls all the rows and then loops over the results, setting them to a multidimensional array where you can then compare the column values for the multiple rows and use that comparison to decide which row to keep. If you're getting multiple entries maybe look into preventing users from double-clicking by using jquery to hide the button or gray it out as soon as it's been clicked. Just some thoughts...