Mysql - Query UPDATE at vnum END in number 7 - mysql

I need to correct this query! Thank!
I try:
UPDATE `item_proto_copy2`
SET `socket_pct`='1'
WHERE `vnum` END AS 7 AND `type`=1

do you mean?
UPDATE `item_proto_copy2`
SET `socket_pct`='1'
WHERE `vnum` = 7 AND `type`=1
if not, follow-up question, what do you want on this condition WHERE vnum END AS 7 AND type=1?
UPDATE 1
UPDATE `item_proto_copy2`
SET `socket_pct`='1'
WHERE RIGHT(`vnum`, 1) = '7' AND `type`=1
RIGHT

#JW's answer works by converting the vnum to a CHAR and then chopping it off with the RIGHT() function, keeping only the first character from the right:
WHERE RIGHT(vnum, 1) = '7'
You could also use LIKE in a similar way (implicit conversion to CHAR and then checking the right-most character):
WHERE vnum LIKE '%7'
And if the numbers are non-negative integers, this would work too, using modular arithmetic:
WHERE vnum MOD 10 = 7

Guessing at what you're trying to do in your where clause. The END does not belong in your WHERE clause and you cannot use AS in a WHERE clause either.
UPDATE `item_proto_copy2`
SET `socket_pct`='1'
WHERE `vnum` = 7 AND `type`=1

Related

MySQL: Is it possible to set a variable outside of SELECT with SELECT as the only permission?

WHAT
I'm on MySQL 5.6 and my account only has permissions for SELECT and USAGE (as found using the SHOW GRANTS command).
Presumably because of this using DECLARE before SELECT returns a generic syntax error.
WHY
I want to use variables to both make the code easier to read and maintain.
Below you can see variable #isOwn is needed in WHERE clause and the day differences could be shortened (note: DATEDIFF didn't work for me in WHERE for some reason)
QUESTION
Being able to declare the variables before SELECT would simplify everything.
Am I doing something wrong or can I not do this because of my minimal permissions?
CODE EXAMPLE
DECLARE #test INT = 5 -- <<< syntax error
SELECT
CASE
WHEN #isOwn := sp.`name` LIKE 'TST %'
THEN 'O' ELSE 'S'
END AS 'Group',
st.`name` AS 'Type',
COUNT(st.`name`) AS 'Count',
DATE(MIN(os.endTime)) AS 'From',
CASE
WHEN #isOwn
THEN TO_DAYS(CURRENT_DATE) - TO_DAYS(MIN(os.endTime)) - 3 + 1
ELSE TO_DAYS(CURRENT_DATE) - TO_DAYS(MIN(os.endTime)) - 28 + 1
END AS 'DO'
FROM tdb.orders AS os
LEFT OUTER JOIN tdb.shipment_type AS st
ON (st.ID = os.shipmentType_ID)
LEFT OUTER JOIN tdb.suppliers AS sp
ON (sp.ID = os.supplier_ID)
WHERE
os.proof IS NULL
AND os.endTime IS NOT NULL
AND ((
sp.`name` NOT LIKE 'TST %' AND
(TO_DAYS(CURRENT_DATE) - TO_DAYS(os.endTime)) >= 3
) OR (
sp.`name` NOT LIKE 'TST %' AND
(TO_DAYS(CURRENT_DATE) - TO_DAYS(os.endTime)) >= 28
))
AND YEAR(os.endTime) = YEAR(CURRENT_DATE)
GROUP BY
CASE
WHEN sp.`name` LIKE 'TST %'
THEN 'O' ELSE 'S'
END,
st.`name`
Use SET keyword instead of DECLARE. The latter is only needed in stored procedures and functions, to define local variables.
You need an user variable, which is different, for example it is not required to define it and it has no strict type. User variables have # prefix, local variables don't. But you can still access it, even from stored procedures/functions.
Also, as commented, you need to separate the two statements (SET and SELECT) with a delimiter ; (or perhaps call them as two statements on the same connection).

Select all records after the CHAR c

I have a column(char) with values between A and Z
I only want to select the records where the char is >= 'C'
Can anyone help me with this?
I tried >= 'C' but this didn't work. Also I couldn't find anything about this on the internet. So I thought it's a good question to ask.
You can use the ascii value for comparison.
select * from tablename where ascii(colname) >= ascii('C')
here is another method.
SELECT SUBSTRING_INDEX(YourColumn,'c',-1) FROM Yourtable;
Strings can be compared in MySQL with regular comparison operators, so this should work:
SELECT * FROM table WHERE col >= 'C'
Do note that the exact sort order (mainly case sensitivity) for strings depends on your characterset collation. Maybe that is the reason why it didn't work for you.
You can also use ASCII() function, which returns the character value of a single character, and compare those:
SELECT * FROM table WHERE ASCII(col) >= ASCII('C')
Note that this only works for single byte characters. For multi byte characters you must use ORD() instead of ASCII().
Yet another way is to use STRCMP() which compares two strings (again, using the sort order of your characterset collation) and returns 0 if the strings are the same, -1 if the first argument is smaller than the second, and 1 otherwise.
SELECT * FROM table WHERE STRCMP(col, 'C') >= 0

Appending a "0" to the front of a string

Someone in my company downloaded some data, played with it in Excel and uploaded again.
Excel when trying to be helpful truncated a leading zero on a file called license_number.
As a result rather than having "037463524" the data now says "37463524"
I know that if the string is eight characters long, I need to add a "0" to the front of it to correct the mess.
Is there a SQL query that I can run in order to accomplish this?
You can use LENGTH()
UPDATE Tablename SET license_number = '0' + license_number WHERE LENGTH(license_number) = 8
or
UPDATE Tablename SET license_number = CONCAT('0', license_number) WHERE LENGTH(license_number) = 8
One more way by using LPAD
UPDATE `TABLE` SET `Lic_NO` = LPAD(`Lic_NO`, 9, '0')

Does MySQL "lazy evaluate" when having queries inside IF (conditional) statements? [duplicate]

I need to query data from a second table, but only if a rare set of conditions in the primary table is met:
SELECT ..., IF(a AND b AND c AND (SELECT 1 FROM tableb ...)) FROM tablea ...
a, b, and c conditions are almost always false, so my thinking is the subquery will never execute for most rows in the result set and thus be way faster than a join. But that would only true if the IF() statement short circuits.
Does it?
Thanks for any help you guys can provide.
The answer is YES.
The IF(cond,expr_true,expr_false) within a mysql query is short-circuited.
Here a test, using #variables to prove the fact:
SET #var:=5;
SELECT IF(1 = 0, (#var:=#var + 1), #var ); -- using ':=' operator to modify 'true' expr #var
SELECT IF(1 = 1, #var, (#var:=#var + 1) ); -- using ':=' operator to modify 'false' expr #var
SELECT #var;
The result is '5' from all three SELECT queries.
Had the IF() function NOT short circuited, the result would be a '5' from SELECT #1, and '6' from SELECT #2, and a '7' from the last "select #var".
This is because the 'true' expression is NEVER executed, in select #1 and nor is the false expression executed for select #2.
Note the ':=' operator is used to modify an #var, within an SQL query (select,from, and where clauses). You can get some really fancy/complex SQL from this. I've used #vars to apply 'procedural' logic within a SQL query.
-- J Jorgenson --
With J. Jorgenson's help I came up with my own test case. His example does not try to short circuit in the condition evaluation, but using his idea I came up with my own test and verified that MySQL does indeed short-circuit the IF() condition check.
SET #var:=5;
SELECT IF(1 = 0 AND (#var:=10), 123, #var); #Expected output: 5
SELECT IF(1 = 1 AND (#var:=10), #var, 123); #Expected output: 10
On the second example, MySQL is properly short-circuiting: #var never gets set to 10.
Thanks for the help J. Jorgenson!
It depends.
IF doesn't short-circuit such that it can be used to avoid truncation warnings with GROUP_CONCAT, for example in:
set ##group_concat_max_len = 5;
select if(true or #var:=group_concat('warns if evaluated'), 'actual result', #var);
the result will be 'actual result' but you'll get a warning:
Warning (Code 1260): Row 1 was cut by GROUP_CONCAT()
which is the same warning you get with less trivial GROUP_CONCAT expressions, such as distinct keys, and without the IF at all.
Try it in the SQL analyzer. If you want to be on the safe side and not have to trust the database to work one way (and not to change that behavior ever in new versions), just make two queries and do the IF programmatically.

update a column by subtracting a value

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