SQL Update Query Issue #1054 - mysql

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

Related

sql query displaying correct values but wrong 'id'

I have an sql query like the following
select *
from register_bs
INNER JOIN spouse_details ON register_bs.reg = spouse_details.reg
WHERE country NOT IN('Australia', 'USA', 'Germany', 'Canada');
This query is displaying correct values for all fields except the 'id' field, it's giving some random id for every data. for example the below data:
when i click on any button of the above data it is going to edit page with different data like below
because the id got is wrong
As there are 900 columns in my register_bs table, I cant use field alias instead of *. Can anyone please tell me how to correct my statement to get the correct id? Thanks in advance
First execute the below query in server and check the values of ID
SELECT register_bs.ID AS ValidateedID, register_bs.*, spouse_details.*
FROM register_bs INNER JOIN spouse_details ON register_bs.reg = spouse_details.reg
WHERE country NOT IN('Australia', 'USA', 'Germany', 'Canada');
Kindly check if the two tables that you have joined have the same column name for their id.
If yes, specify the table first and then the column of the id you want to retrieve.
e.g. register_bs.id

How to update a column name which is named after a date in SQL

UPDATE studentattendance
JOIN studentdetails using(matricno)
JOIN studentModules using (matricno)
SET 05/12/15 = 1
WHERE cardUid = '01545695'
Hey hows it going, I'm having trouble updating a column in my database, the column is called '05/12/15' because it's named after a date. However I'm having trouble updating it with SQL because the syntax is wrong.
How do I update it as it seems to be having trouble with the forward slashes. I've tried single quotes and square brackets but they don't work
Thanks
Using backticks permits you to use alternative characters.
SET `05/12/15` = 1
query :-
UPDATE studentattendance
JOIN studentdetails using(matricno)
JOIN studentModules using (matricno)
SET `05/12/15` = 1
WHERE cardUid = '01545695'

How do I copy multiple values from within the same table to other values in that same table in SQL?

First of all I'm rather new to SQL and so even though I believe a similar question was asked in this thread ( SQL Query - Copy Values in Same Table ) I literally can't understand it well enough to utilize the information. For that I apologize.
Now, I have a table that looks something like this:
company id | parameter name | parameter title
P | Parameter One | First Parameter
P | Parameter Two | Second Parameter
P | Parameter Three| Third Parameter
W | Parameter One | NULL
W | Parameter Two | NULL
Except that my table obviously has quite a lot of rows. I already went through filling in all the parameter titles where the company id was 'P' and would like to avoid manually doing the same for those with company id 'W'. My question is what SQL statement (this is in Microsoft SQL Server 2008) can I use to copy the values in the column "parameter title" where the company id is 'P' to the values in the same column where the company id is 'W' and both parameter names match up (W has less parameters than P)?
Using the previously linked thread I was able to come up with the following, but it spits out an error and I know it's not done correctly:
UPDATE COMP_PARAMETER_COPY
SET PARAM_TITLE=(SELECT PARAM_TITLE FROM COMP_PARAMETER_COPY P
WHERE P.COMP_ID = 'P' AND P.PARAM_TITLE=PARAM_TITLE)
WHERE COMP_ID='W'
(I'm playing around with a copy of the table instead of the actual table)
The error I get is "Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated."
Thank you for your help and advice,
-Asaf
You need to ensure that your subquery is only returning one result. Right now that error message is telling you that you're getting more than one record returned.
UPDATE W
SET PARAM_TITLE = (
SELECT PARAM_TITLE FROM COMP_PARAMETER_COPY P
WHERE P.COMP_ID = 'P' AND P.PARAM_NAME = W.PARAM_NAME
)
FROM COMP_PARAMETER_COPY W
WHERE W.COMP_ID = 'W'
Try giving the above SQL a whirl. This could still give you more than one result, but without knowing what your table looks like and what the data constraints are it's hard to give you something guaranteed to work.
Try adding the DISTINCT keyword to your query:
UPDATE COMP_PARAMETER_COPY
SET PARAM_TITLE=(SELECT DISTINCT PARAM_TITLE FROM COMP_PARAMETER_COPY P
WHERE P.COMP_ID = 'P' AND P.PARAM_TITLE=PARAM_TITLE)
WHERE COMP_ID='W'

MySQL query to remove certain email address, all follow similar pattern

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'

Update Value in Table from another table

I realized that i was using a varchar attribute as a index/key in a query, and that is killing my query performance. I am trying to look in my precienct table and get the integer ID, and then update my record in the household table with the new int FK, placed in a new column. this is the sql i have written thus far. but i am getting a
Error 1093 You can't specify target table 'voterfile_household' for update in FROM clause, and i am not sure how to fix it.
UPDATE voterfile_household
SET
PrecID = (SELECT voterfile_precienct.ID
FROM voterfile_precienct INNER JOIN voterfile_household
WHERE voterfile_precienct.PREC_ID = voterfile_household.Precnum);
Try:
update voterfile_household h, voterfile_precienct p
set h.PrecID = p.ID
where p.PREC_ID = h.Precnum
Take a look at update reference here.
Similarly, you can use inner join syntax as well.
update voterfile_household h inner join voterfile_precienct p on (h.Precnum = p.PREC_id)
set h.PrecID = p.ID
What if the subquery returns more than one result? That's why it doesn't work.
On SQL Server you can get this type of thing to work if the subquery does "SELECT TOP 1 ...", not sure if mysql will also accept it if you add a "limit 1" to the subquery.
I also think this is pretty much a duplicate of this question ("Can I have an inner SELECT inside of an SQL UPDATE?") from earlier today.
Firstly, your index on a varchar isn't always a bad thing, if it is not a key you can shrink how much of the field you index to only index say the first 10 chars or so.
Secondly, it won't let you do this as if it is a set that is returned it could break.