I have a PostgreSQL table with a text field with a default value of an empty string ('') that will be a list of tags. I am trying to update that field by appending a string using CONCAT, but it is not working. This is my SQL:
UPDATE pins SET tags = concat(tags, :tagString) WHERE id = :id
$tagString would look something like this: [5378]
Similar queries have worked like this:
UPDATE pins SET tags = :tagString WHERE id = :id
It seems that the problem is the concat function. What is wrong?
Related
I actually want to search the hashtags with this MySQL query.But its not working for me, I don't know why.
Here is my code. My code uses a PHP variable inside
$searchtext = "#truth";
"SELECT id FROM bp_activity WHERE content REGEXP '[[:<:]]'". $searchtext ."'[[:>:]]'"
This is returning empty but one of the rows in the table contains this expression like this:
Unveilling the #truth
I think it should be sorted out.
But when I only use this this it works
"SELECT id FROM bp_activity WHERE content REGEXP '[[:<:]]truth[[:>:]]'"
But it doesn't work when using like this
"SELECT id FROM bp_activity WHERE content REGEXP '[[:<:]]#truth[[:>:]]'"
Right now Im using the following script to find and replace text in a MySQL database using phpmyadmin and it works fine.
For a single table update
UPDATE 'table_name'
SET 'field_name' = replace(same_field_name, 'unwanted_text', 'wanted_text')
What I want now is to find entries that contains a specified string, and then replace the whole field.
So for example, If I have the following entries:
ABC_1234
ABC_123456789
XYZ_1234
And I want to look for the entries that contains "ABC_" and replace the entire field to "FGH-432"
The result of the desired script will give the following output.
ABC_1234 > FGH-432
ABC_123456789 > FGH-432
XYZ_1234 (No change)
What modification should I make to the script?
You could do:
UPDATE `table_name`
SET `field_name` = 'FGH-432'
WHERE `field_name` LIKE 'ABC\_%';
How to search for a comma-separated string in a database table in mySQL.
Suppose, I have a string in variable $facilities='breakfast,dinner,lunch' and in my database I have a string saved in a field called facilities having values breakfast,dinner,clothing,lunch,hot water.
How do I get the row having values $facilities?
Work like this i hope this is your requirement
select * from table where field_name like '%$fieldname%'
You're going to need to break apart the comma separated string first, and include a clause for each, so for that input you'd generate SQL like (assuming you want rows that have all three, and maybe other flags set):
SELECT *
FROM YourTable
WHERE FIND_IN_SET('breakfast', facilities)
AND FIND_IN_SET('dinner', facilities)
AND FIND_IN_SET('lunch', facilities)
You can break the string apart in PHP with something a little like:
$facilities='breakfast,dinner,lunch';
$desired = explode ( "," , $facilities );
And just use a loop to build your SQL
SELECT *
FROM tableName
WHERE FIND_IN_SET('lunch', 'breakfast,dinner,lunch')
EDIT:
SELECT *
FROM tableName
WHERE FIND_IN_SET('lunch', food_column)
food_column is the name of the column where the string 'breakfast,dinner,lunch' is stored
My database contains empty table columns.
I would like to add a character like § to these empty rows so that I can search for them easier. How would I go about?
I already have a script that lets me replace or remove characters but I dont know a way to specify that rows that are empty should be updated with a character.
First, you probably don't have empty rows but empty column values in the rows. Wouldn't it be better if you just do it like if (!empty($row['column'])) instead of trying to put some bogus character?
Or if you want to do a SELECT just do something like this:
SELECT * FROM table_name WHERE column_name > ''; // seems to work for both NULL and empty string
Or:
DELETE FROM table_name WHERE column_name IS NULL or column_name = '';
UPDATE `table` SET column = "§" WHERE column = "";
It's bad to add character to an empty column because you are only adding extra size to the database. It's easy to search empty string on the database. Possible solutions of searching will be using of IS NULL to search for null columns.
SELECT * FROM tableName WHERE collName IS NULL
Another is by using CHAR_LENGTH (which gets the length of the data in the column)
SELECT * FROM tableName WHERE CHAR_LENGTH(collName) = 0
or by simply comparing it to ''
SELECT * FROM tableName WHERE colName = ''
I have some rows ( > 50.000) in my database which contains columns with strings + '\n'. I mean the records looks like this:
abcd\n or \nabcd
Because of that sometimes my application does not work correctly. My question is: How could i update this rows with the correct value and eliminate '\n' spaces besides going to every row and update it manually ?
update your_table set somecolumn = replace(somecolumn, '\n', '')
update mytable SET title = TRIM(TRAILING '\n' FROM title)
Have you tried this?
UPDATE table SET column=REPLACE(column,'\\n','') WHERE column LIKE '%\\n' OR column LIKE '\\n%'
If you don't know where the new line is, simply:
UPDATE table SET column=REPLACE(column,'\\n','')