I have a replace statement to replace numerical values in columns with text, the text may contain numbers, how to avoid re-replacing these numbers?
update
`obsmaintble`
set
`obsvteduration` = REPLACE(`obsvteduration`, '0', 'No C-Section'),
`obsvteduration` = REPLACE(`obsvteduration`, '1', 'LOS equal or more than \4 - Given up to discharge'),
`obsvteduration` = REPLACE(`obsvteduration`, '2', 'LOS equal or more than \4 - Given for < \4 days'),
`obsvteduration` = REPLACE(`obsvteduration`, '3', 'LOS less than \4 Given up to discharge'),
`obsvteduration` = REPLACE(`obsvteduration`, '4', 'Not given'),
Related
I have a database SQL table looking like this
[the database table]
So I'm trying to update this table simultaneously, such that one query updates multiple columns in multiple rows at once. The script looks like this
INSERT INTO teams
(id, team_name, matches_played, won, lost, points, goals_for, goals_against, goal_difference, last_match)
VALUES
('10', 'Manchester City', '3', '1', '', '3', '2', '0', '0', 'W'),
('5', 'Westham', '2', '', '1', '3', '0', '2', '-1', 'L')
ON DUPLICATE KEY UPDATE
team_name = VALUES(team_name),
matches_played = VALUES(matches_played),
won = VALUES(won),
lost = VALUES(lost),
points = VALUES (points),
goals_for = VALUES(goals_for),
goals_against = VALUE(goals_against),
goal_difference = VALUE(goal_difference),
last_match = VALUES(last_match);
My issue is. This code works perfectly on XAMPP MySQL server but not on workbench. What might the issue be?
In MySQL, I run a lot of SELECT statements with IN clauses. Sometimes I want to see the output table ordered by the order of values that I used in the IN clause, rather than alpha/numerical.
If I ran
SELECT id FROM x
WHERE id IN ('1', '3', '2', '0')
I would want output 1, 3, 2, 0, not 0, 1, 2, 3.
Is there a way to effect this?
Thanks!
With the following query you should get what you want:
SELECT id FROM x WHERE id IN ('1', '3', '2', '0') ORDER BY FIELD(id, '1', '3', '2', '0')
From the MySQL specification about FIELD():
Returns the index (position) of str in the str1, str2, str3, ... list. Returns 0 if str is not found.
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_field
I want to tab space my data while inserting through sql in phpmyADMIN.
What should I do?
INSERT INTO `questions` (`id`, `question_name`, `answer1`, `answer2`, `answer3`, `answer4`, `answer5`, `answer6`, `answer`, `category_id`) VALUES(90,'C<br> java', '1', '2', '3', '4', '', '', '3', 2);
You can split the string up and place a tab in between: 'some' + CHAR(9) + 'string'. Note that 9 is the ASCII code for a horizontal tab. Perhaps easier is 'some\tstring', which is syntax common to many other languages.
See table 9.1 here: http://dev.mysql.com/doc/refman/5.0/en/string-literals.html
i am trying to calculate the values for a new column impressions which is equal to the sum of ( sum_retweet and sum_reply ) multiply by the value in the column followers_count. so for the first row it would be : (2+1)*812 but on condition that the total sum of sum_retweet and sum_reply must be greater than zero. If the sum is equal to zero than impressions will = to just the followers_count.
account_id, date, user_screenname, sum_retweet, sum_reply, followers_count, Reach
'9', '2008-06-11', 'A', '2', '1', '812', '1624'
'9', '2008-06-12', 'B', '0', '1', '813', '813'
Here is my current code:
CREATE VIEW `tweet_sum` AS
select `tweets`.`account_id` AS `account_id`,
`tweets`.`user_screenname` AS `user_screenname`,
CAST(`tweets`.`datetime` as date) AS `period`,
MAX(`tweets`.`followers_count`) AS `followers_count`,
SUM(`tweets`.`is_reply`) AS `sum_reply`,
SUM(`tweets`.`is_retweet`) AS `sum_retweet`,
MAX(`tweets`.`followers_count`) * ((SUM(`tweets`.`is_reply`) > 0) + (SUM(`tweets`.`is_retweet`) > 0)) as reach
from `tweets`
group by cast(`tweets`.`datetime` as date), tweets.username;
HOw do i add the calculations for the impressions column in?
Use a case statement for this:
case when SUM(`tweets`.`is_reply`) + SUM(`tweets`.`is_retweet`) > 0
then (SUM(`tweets`.`is_reply`) + SUM(`tweets`.`is_retweet`))
* `tweets`.`followers_count`
else `tweets`.`followers_count` END as newColumn
This would do it in SQL, but this syntax may not work in mysql (dont know):
CASE WHEN (Sum_Retweet + Sum_Reply) > 0
THEN (Sum_Retweet + Sum_Reply) * followers_count
ELSE followers_count
END
Field names would need to be changed to your actual field names.
I have an auto-increment transactionID type=MEDIUMINT(9) in my table. I want to also display a unique 4-character (which can grow over time, but 4 for now) alphabetical Redemption Code to my users. What is the best way to derive this alphabetical code from my transactionID, preferably straight from the SELECT statement?
That mostly depends on what alphabet you want to use.
You may use TO_BASE64 to convert it it to base64 encoded string or simply do something like:
select REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(your_number, '0', 'A')
, '1', 'B')
, '2', 'C')
, '3', 'D')
, '4', 'E')
, '5', 'F')
, '6', 'G')
, '7', 'H')
, '8', 'I')
, '9', 'J')
if you want custom alphabet.
In case you want something shorter, you can go a slightly harder way:
You use 9-digit decimal (maximum 999999999), which translates to 8 hex digits (0x3B9AC9FF), i.e. 4 bytes. What you can do is divide your number in 4 binary octets, convert them to chars, construct new string and feed it to TO_BASE64():
select TO_BASE64(CONCAT(CHAR(FLOOR(your_number/(256*256*256))%256),CHAR(FLOOR(your_number/(256*256))%256),CHAR(FLOOR(your_number/256)%256),CHAR(your_number%256)))
Note, that TO_BASE64() function is available only in MySQL 5.6 on-wards.
Now, for those on older versions - we don't want to implement base64 encoding with our bare hands, don't we? So, lets go the easier way: we have 30 bits in those 9 decimal digits, which would be 30/6=5 characters, if we use 64 continuous character alphabet after CHAR(32), which is space, which we don't want to use:
SELECT CONCAT(`enter code here`CHAR(FLOOR(your_number/(64*64*64*64))%64+33),CHAR(FLOOR(your_number/(64*64*64))%64+33),CHAR(FLOOR(your_number/(64*64))%64+33),CHAR(FLOOR(your_number/64)%64+64),CHAR(your_number%64+33))
I was just looking for something like this and I found a way to do it with the CONV function.
CONV(9+your_number, 10, 36)
This converts 1 to A, 2 to B etc.
The way it works is by adding 9 and then converting to base 36, in which 10 is A, 11 is B etc.
You can generate a hash with the value of transaction id.
Like:
SELECT MD5(transactionID) FROM `yourtable`;
There are several other types of similar functions you can use.
Maybe can use CASE WHEN() END; and stored procedure
For example:
CREATE DEFINER = 'USERNAME'#'HOST' STORED PROCEDURE `ConvertNumberToAlphabetic`
BEGIN
SELECT
(CASE
WHEN (your_number = '1') THEN 'A'
WHEN (your_number = '2') THEN 'B'
WHEN (your_number = '3') THEN 'C'
WHEN (your_number = '4') THEN 'D'
WHEN (your_number = '5') THEN 'E'
WHEN (your_number = '6') THEN 'F'
WHEN (your_number = '7') THEN 'G'
WHEN (your_number = '8') THEN 'H'
WHEN (your_number = '9') THEN 'J'
WHEN (your_number = '10') THEN 'K'
END) RedeemCode
FROM tblTransaction;
END
SELECT SUBSTRING(MD5(transactionId) FROM 1 FOR 4) AS RedemptionCode
This creates a 4 character (easy to change, as you can see) string where the characters are from the MD5 command (and therefore in the range a-z and 0-9).