Wondered if someone could help me write a MySQL query. I noticed in my email database I have a huge amount of users who got past my automated entry checks who I want to flag. They are all of the form abcdef123#hotmail.com where abcdef are random names of variable length, then a 3 digit number.
I have a field in my table called fld_bad, which I want to change to 1 in the query.
So something like
UPDATE tbl_users SET fld_bad = "1" WHERE fld_email .....
Obviously the ..... is where my knowledge is failing me!
you can use the mysql regexp command to do this
http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
UPDATE tbl_users SET fld_bad = "1" WHERE fld_email REGEXP '[A-Za-z]+[0-9]{3}#hotmail\\.com' = 1;
You can use:
UPDATE tbl_users
SET fld_bad = "1"
WHERE fld_email REGEXP '[[:alpha:]]+[[:digit]]{3}#hotmail\\.com'
Related
I don't code often and am stuck on something that should be simple. I'm trying the following:
UPDATE `wp_usermeta`
SET `class-count` = class-count+1
WHERE user_id = 7
but am getting an error unknown column for class count. Class-count is the record identifier in the column meta_key and the user_id is in the column meta_key as well.
What am I doing wrong? I just want to be able to enter the user_id into a text field on a form and update the class-count by 1 on each submission.
#Michael Smith, please check the column name i.e. class-count. Because SQL will not accept like class-count, it should be like class_count.
In sql columns to separate two words _ should be used instead of -. So try to use class_count.
Other thing is that count is reserved keyword for sql so you can write like this
[classcount]
You can try this code
UPDATE `wp_usermeta` SET `class-count` = `class-count`+1 WHERE
user_id =7
I need advices in order to make a process on my list of values. I have a table llx_societe and some fields where one of them is code_client. This field looks like :
0099
00100
00101
00102
...
00998
00999
001000
I want to remove the first zero for all values between 00100 and 00999 in order to get 0100 until 0999.
I wrote this command :
UPDATE `llx_societe`
SET `code_client`= SUBSTR(code_client,1)
WHERE `code_client` BETWEEN '00100' AND '00999';
But nothing, none lines are proceed.
Have you an explanation ?
SQL starts counting from 1 and not 0. Try this:
UPDATE `llx_societe`
SET `code_client`= SUBSTR(code_client,2)
WHERE `code_client` BETWEEN '00100' AND '00999';
Try this:
UPDATE llx_societe
SET code_client= SUBSTR(code_client, 2)
WHERE code_client between '00100' AND '00999'
MySQL SUBSTR() function
You can use the following SQL:
UPDATE TABLENAME SET data = SUBSTR(FIELD, 2);
for example if there is table(userinfo) and field is username
UPDATE users SET username = SUBSTR(username, 2);
This is my first time creating a MySQL stored procedure and am stuck on getting the UPDATE piece to work correctly. The proc is performing an inner join, looking for matches on a domain name field. If there is a match, a column named inbound is getting updated with a value of 0. If there is not a match on the join, then I need inbound set to a value of 1.
When I run this, I am able to get the matches tagged with a 0, but the non-matches are not getting updated with a 1. I thought how I have the 'ELSE' part set up would take care of this- can anyone tell if I am missing something with the syntax?
CREATE PROCEDURE `sp_InboungTagging`()
BEGIN
update `tableA` a
inner join `TableD` d
on a.senderDomain = d.domainName
set inbound = CASE
when a.senderDomain = d.domainName then 0
ELSE 1
END
WHERE inbound is null;
END;|
DELIMITER ;
Thanks,
Ron
EDIT-
Thanks for your reply. I am looking for exact matches on a varchar field that has domain names in it- the master list of domains is in table D. If the record in TableA has a match in TableD, I want to tag that recored with a 0. If there is no match in TableD, then I would like to tag it with a 1. Let me know if that clears things up- thanks
Your JOIN condition is the same as your CASE condition. If you JOIN your two tables on:
a.senderDomain = d.domainName
Then there will be no values in the result set for which
a.senderDomain != d.domainName
so the ELSE clause of your CASE statement never fires.
Without knowing more about what you mean by "matches" and "non-matches," I can't really suggest a correction.
I am updating a field in a mysql column namend "frontpage", set it from 0 to 1.
No problem with this query:
mysql_query("UPDATE table SET frontpage='1' WHERE user_id='999' AND poll_id='555'");
What I'd like to accomplish is, in case user_id 999 got already other existing poll_id's set to 1 in the past, these rows should be set to 0 automatically.
As a beginner learning MySQL, I would run 2 queries, first one to set everything to frontpage='0' WHERE user_id='999' and the second query to set frontpage='1' WHERE user_id='999' AND poll_id='555'.
My question now is, could this be done by using only one query, and how?
PS: Not sure if it has something to do with my question, I've read these answers MySQL: Updating all rows setting a field to 0, but setting one row's field to 1 but I haven't really understood the logic, perhaps someone can explain it to a mysql beginner please.
I think you want this logic:
UPDATE table
SET frontpage = (case when poll_id = '555' then '1' else '0' end)
WHERE user_id = '999';
As a note: if the constants should really be integers, then drop the single quotes. In fact, you can then simplify the query to:
UPDATE table
SET frontpage = (poll_id = 555)
WHERE user_id = 999;
I am using this part of a sql statement to delete accounts with the specific phone number in a cronjob.php file. How do I alter this statement to test for the length of a specific given field in the user-profies, rather than what I list here as the value being LIKE '%2147483648%'
$strSQL = "DELETE
FROM users,
profile_values
USING profile_values
INNER JOIN users USING(uid)
INNER JOIN profile_fields USING(fid)
WHERE profile_values.uid=users.uid
AND profile_values.value LIKE '%2147483648%'
AND (profile_fields.name = 'profile_phone_number')";
execSQL($strSQL);
If you are testing for length of the data in the field, you can use code like:
... WHERE LENGTH(TRIM(profile_values.value)) = 10 ...
substituting the correct field name. The TRIM function is important to eliminate any leading or trailing spaces which might have crept in to the data.
Be careful using a function like this to automatically delete users. It will be quite indiscriminate.