I'm trying to come up with a MySQL query that will update points... Can I do something like this?
UPDATE `a75ting`.`username` SET `points` = '`points` - 5'
UPDATE a75ting.username
SET points = points - 5
by putting the single quotes around the "points -5", you converted that expression into a plaintext string. Leaving it without the quotes lets MySQL see you're referring to a field (points) and subtracting 5 from its current value.
Run this query to find out the difference:
SELECT '`points` - 5' AS string, `points` - 5 AS expression
FROM a75ting.username
Related
I have tried to select something with SQL, and I've a problem with it.
What I want:
SQL SELECT * FROM table WHERE ? = '5';
Select everything which = 5, BUT not specify from which column.
Example:
From this ""database"", you should receive the 1st and the last row.
Is that possible?
You have to list the columns but you can use in. The where clause looks like:
where 5 in (price, height)
Note: This assumes that the columns have the same type. You could get type conversion errors if they are not.
Also, given the names of the column and the data, I assume that the columns are stored as numbers. Hence, I dropped the single quotes around 5. If they are really strings, then use the single quotes.
you need to add a condition to your query with or keyword so if any of them match the row will be shown as a result
SELECT * FROM tablename WHERE price =5 or height= 5
better you list your columns by name instead of using * after SELECT
I am running a query on a column postal (type double).
SELECT * FROM `table` WHERE `postal` LIKE 'abcdef'; # returns 1 record
and the same query using = returns 100+ records.
SELECT * FROM `table` WHERE `postal` = 'abcdef'; # returns 107 record
What could be the reason?
You are using LIKE on a DOUBLE field, you should not do that.
LIKE is reserved for pattern matching on strings. Use = for numbers, or convert your digit to a string first using CONVERT and then apply the logic with LIKE.
= compares two values for identity.
LIKE is for pattern matching ie. that is, it matches a string value against a pattern string containing wild-card characters.
Refer here
LIKE will check and return similar values where as = will check for the exact value.
The following things affects the result (not the complete list!)
Implicit conversation
MySQL extension to standard SQL's LIKE operator
In each cases an implicit conversion occours: MySQL tries to convert the values to a common data type. In the first case case 'abcdef' will be converted to double which results to 0. This is why you get 107 records when comparing with equals (=).
SELECT * FROM `table` WHERE `postal` = 'abcdef'; # returns 107 record
You should get exactly the same result by running
SELECT * FROM `table` WHERE `postal` = 0;
In MySQL, LIKE is permitted on numeric expressions. (This is an extension to the standard SQL LIKE.)
This means that SELECT CASE WHEN 10 LIKE '1%' THEN 1 ELSE 0 END is allowed and results to 1 (matched)
To be honest, I'm not sure which double value could match with LIKE operator with the pattern 'abcdef'.
Basically I have a table called 'Telephone' with over 100,000+ records.
In this database I have numbers such as:-
90123456789
91234567893
97126372319
Basically I want to Update the Telephone.DB and SET the numbers so it strips the first number only if it starts with the number '9', so the result would end up as follows:-
0123456789
1234567893
7126372319
Any idea how I can achieve this using MySQL?
Provided that the datatype is a string type, this should do it;
UPDATE numbers
SET number = SUBSTR(number, 2)
WHERE number LIKE '9%';
An SQLfiddle to test with.
As always, test yourself before running updates from random people on the Internet on your production database :)
Ok, this works for a VARCHAR column. You could always convert on the fly the value to varchar:
SELECT CONCAT(REPLACE(SUBSTR(PHONEFIELD,1,1),9,''), SUBSTR(PHONEFIELD,2)) AS PHONEWITHOUTNINE FROM TABLENAME
Check it out and tell me if it works.
I am looking for some help.
I am running the following in mySQL
"SELECT AVG(`readingValue`) AS `readingValue` FROM table
This returns the value - 0.0282982
If possible I would like to return only 4 digits rounded up/down to closest.
In this case 0.0283.
Is this possible in the select string, or will I need to do it in php?
p.s readingValue column is float(4,3)
Would the ROUND function be an option?
SELECT ROUND( AVG(`readingValue`), 4 ) AS `readingValue` FROM table
Hi I am trying to get the following sql to work in mysql but it always return an empty result set - however there are definitely entries that match the criteria.
I'm new to mySQl so would appreciate if someone could point out where I am going wrong.
SELECT * FROM `ch_results`
WHERE 'readingDateTime' = '2011-03-29 20:00:00'
Remove the quotes around the field name:
SELECT *
FROM `ch_results`
WHERE readingDateTime = '2011-03-29 20:00:00'
Your current query compares string 'readingDateTime' to another string, '2011-03-29 20:00:00', which comparison of course never holds true.
Drop the quotes on the 'readingDateTime'. This is comparing strings to each other.
WHERE readingDateTime = '2011-03-29 20:00:00'