How to update with concat and if statement? [duplicate] - php

This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 3 years ago.
I have a table with contact codes. When contact code is not specified there are ^^ in the empty cell. If specified should be: ^Code123^,^Code321^,^Code987^
I want to update this cell, but I dont know how to rewrite empty cell with ^^?
With my query I get this result ^^,^Code123^,^Code321^,^Code987^. How to delete ^^ when updating cell?
$sql6 = "UPDATE contacts
SET clicks_c=IF(clicks_c='',
'^".$url_name."^',
CONCAT_WS(',',clicks_c,'^".$url_name."^'))
WHERE id_c='".$row_id."'";

If an empty cell contains ^^, you need to test for that in your IF(). Change:
IF(clicks_c='',
to:
IF(clicks_c IN ('', '^^),
Then it will replace ^^ with a nonempty code, instead of concatenating to it.

Related

Mysql query to get the comma separated value (1,10,15) stored in a column with WHERE Condtion that also has a comma separated Value (15,1,100) [duplicate]

This question already has answers here:
SQL query to match a comma-separated string against a comma-separated string?
(3 answers)
Closed 5 years ago.
My Table Look like Below, Please refer Screenshot.Table Example Structure
I want a query to get the "id" when I pass the comma separated values (ex:25,10,3) in WHERE Condition of user_id column.
EX: Select id from table where user_id IN('25,10,3')
When I execute the above query I should get ID's 1 and 2.
I tired with "WHERE IN" and "find_in_set()". But nothing worked out...
Please help me to get this done...
Balachander,
find_in_set will work (see SQLFiddle), but you should reconsider using proper database structure.
Finally I have Find a Solution for this....
select * from test where user_id REGEXP '(^|,)(25|10|3)(,|$)'
Refer SQLSQLFiddle

COUNT values in a column and store count in variable? [duplicate]

This question already has answers here:
How can I store the result of an SQL COUNT statement in a PHP variable
(4 answers)
Closed 8 years ago.
I have tried a few things and googled but can't get something that works. I want to use an SQL query to count how many rows in a table has "Rock" as it's value inside the "Genre" column. And then I need to store the count number inside a variable so that I can echo it later in my page. Any help? I'm new to php...
A query like this
$lo_rec = mysql_query("SELECT COUNT(`genre`) as `total`
FROM `tablename`
WHERE `genre` = 'ROCK'");
$result = mysql_fetch_object($lo_rec);
Then when you grab the results, assign it
$li_count = $result->total;
echo $li_count;
And before anyone says so, I know to use mysqli or PDO :-)

Getting all effected rown in MySQL UPDATE [duplicate]

This question already has answers here:
UPDATE/DELETE in mysql and get the list of affected row ids?
(3 answers)
Closed 9 years ago.
Title pretty much says it. I am using the deprecated mysql_ functions. I've got a query like:
UPDATE `table`
SET `row` = 'newtext'
WHERE `row` = 'oldtext'
Is there an easy way to get all effected row's primary key? Some glorious combination of mysql_insert_id and mysql_affected_rows? How can I do this without looping and doing each update one row at a time?
As a solution to your problem please refer the following sample code snippet
UPDATE `table` SET `row` = 'newtext' WHERE `row` = 'oldtext'
select id from table where row='oldtext'

how can we avoid duplicate values in mysql database [duplicate]

This question already has answers here:
How to avoid duplicates while updating a MySQL database?
(5 answers)
Closed 8 years ago.
I am storing number of check boxes with delete ,add,view options in database,
if you check on that options,it storing some values.But if i click again that will store again and again.
how to avoid this duplicate values
You can set the column containing unique value as UNIQUE or do a condition :
if SELECT Query return this value , don't insert , else insert.

MySQL: How to pre-append text to data in an existing column? [duplicate]

This question already has answers here:
How to prepend a string to a column value in MySQL?
(5 answers)
Closed 9 years ago.
I have a populated MySQL TABLE where I need to pre-append the text "NEW_" for the name field. (Long story, but this is what needs to be done for now.)
I imagine this can be done with an UPDATE but I'm not sure how to add "NEW_" to the start of the 'name' column. I also work in PHP, so I need to do this in PHP where I read each record, extract 'name' field, add this and then do an UPDATE for the record? Or can this all be done in MySQL?
use this query
UPDATE table_name set `name` = CONCAT('NEW_',`name`)
You can use CONCAT like this:
UPDATE tbl SET name=CONCAT('NEW_',name)
WHERE name NOT LIKE 'NEW\_%';
The second line will stop this operating on columns with NEW_ already at the start.
You can just do it like this:
UPDATE myTable SET name = CONCAT('NEW_',name);
As posted before by myself this will not work:
UPDATE myTable SET name = 'NEW_' + name;

Categories