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
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?
I am trying to use this query, but when there are the same value in different columns, I receive this error:
1060 - Duplicate column name "123"
For example here:
INSERT INTO chiro(in_out,chirocov,chirocov2,chiroded,chiromet,
chirocovp,chirooop,chirooopmet,chirooopcp,
chirovisit,chirouse,chiromax,chirodedapply,
chironum1,chironum2)
SELECT * FROM (SELECT 'in', 'no','individual','123','123','20',
'213','21243','10','14','5','2000','yes',
'0','1') AS tmp
WHERE NOT EXISTS (SELECT in_out,chirocov,chirocov2,
chiroded,chiromet,chirocovp,chirooop,
chirooopmet,chirooopcp, chirovisit,chirouse,
chiromax,chirodedapply,chironum1,chironum2
FROM chiro
WHERE chirocov='no'
AND chirocov2='individual'
AND chiroded='123'
AND chiromet='123'
AND chirocovp='20'
AND chirooop='213'
AND chirooopmet='213'
AND chirooopcp='10'
AND chirovisit='14'
AND chirouse='5'
AND chiromax='2000'
AND chirodedapply='yes'
AND chironum1='0'
AND chironum2='1')
LIMIT 1
But when i change the value, there won"t be any errors. like:
INSERT INTO chiro(in_out,chirocov,chirocov2,chiroded,
chiromet,chirocovp,chirooop,chirooopmet,
chirooopcp, chirovisit,chirouse,chiromax,
chirodedapply,chironum1,chironum2)
SELECT * FROM (SELECT 'in', 'no','individual','123','231','20',
'213','21243','10','14','5','2000',
'yes', '0','1') AS tmp
WHERE NOT EXISTS (SELECT in_out,chirocov,chirocov2,
chiroded,chiromet,chirocovp,chirooop,
chirooopmet,chirooopcp, chirovisit,chirouse,
chiromax,chirodedapply,chironum1,chironum2
FROM chiro
WHERE chirocov='no'
AND chirocov2='individual'
AND chiroded='123'
AND chiromet='123'
AND chirocovp='20'
AND chirooop='213'
AND chirooopmet='213'
AND chirooopcp='10'
AND chirovisit='14'
AND chirouse='5'
AND chiromax='2000'
AND chirodedapply='yes'
AND chironum1='0'
AND chironum2='1')
LIMIT 1
Could you help me and let me know what I am doing wrong?
Just remove the outer SELECT from:
SELECT * FROM (SELECT 'in', 'no','individual','123','123','20',
'213','21243','10','14','5','2000','yes',
'0','1') AS tmp
because it retrieves all the unnamed columns from the inner select with names that are their values, so there are 2 columns with the same name 123.
See a simplified demo of the problem.
Use this:
INSERT INTO chiro(
in_out, chirocov, chirocov2, chiroded, chiromet, chirocovp, chirooop, chirooopmet, chirooopcp,
chirovisit, chirouse, chiromax, chirodedapply, chironum1, chironum2
)
SELECT 'in', 'no', 'individual', '123', '123', '20', '213', '21243', '10', '14', '5', '2000', 'yes', '0', '1'
WHERE NOT EXISTS(
SELECT in_out, chirocov, chirocov2, chiroded, chiromet, chirocovp, chirooop, chirooopmet, chirooopcp,
chirovisit, chirouse, chiromax, chirodedapply, chironum1, chironum2
FROM chiro
WHERE chirocov='no' AND chirocov2='individual' AND chiroded='123' AND chiromet='123' AND chirocovp='20'
AND chirooop='213' AND chirooopmet='213' AND chirooopcp='10' AND chirovisit='14' AND chirouse='5'
AND chiromax='2000' AND chirodedapply='yes' AND chironum1='0' AND chironum2='1'
)
Also LIMIT 1 is not necessary.
I have the following table data:
content,count
'a', '1'
'b', '2'
'c', '2'
'd', '2'
'content_name', '18'
'de', '2'
'dea', '2'
'deaasdfdsaf', '5'
'content_name', '17'
I would like to have the correct query for getting x (for example 4) rows with the biggest values in their content column
I have tried the answers given in:
Getting Top 3 from Mysql table based on Condition/Value
but it didnt work as i was expecting
it keeps giving me just the rows by their table location
any suggestions?
create table Test(content varchar(100),count integer );
insert into Test(content, count) values("a", 1);
insert into Test(content, count) values("b", 2);
insert into Test(content, count) values("c", 2);
insert into Test(content, count) values("d", 2);
insert into Test(content, count) values("content_name", 18);
insert into Test(content, count) values("de", 2);
insert into Test(content, count) values("dea", 2);
insert into Test(content, count) values("deaasdfdsaf", 5);
insert into Test(content, count) values("content_name", 7);
select * from Test
order by count desc
limit 4;
However, I am not quite sure what you mean by "biggest values in their content column", therefore I ranked by their count values.
If you are actually looking for the number of character within the content column, which was my initial impression reading your question:
Using Martin Stagl's post to create the example table .
select *, LENGTH(REPLACE(content, 'N', '')) as char_length from Test;
NOTE: If this is not working on the server, try replacing the LENGTH() function with LEN()
Fiddle: http://sqlfiddle.com/#!9/032d48/9
I have a huge table with 100 fields. Each row is timestamped. I want to find the latest non null value for all columns. I'm using MySql 5.6 InnoDB
e.g.
create table tester(
pub_id int,
pub_name varchar(20),
pub_city varchar(20),
country varchar(20),
no_of_branch varchar(20),
estd datetime
);
insert into tester (pub_id, pub_name, pub_city, country, estd)
values
(1, 'a', 'xyz', 'abcity' , 'a', '1970-01-01 00:00:01'),
(2, 'a', 'xyz', '' , 'a', '1971-01-01 00:00:01'),
(3, 'a', 'xyz', 'abcity1', 'b', '1972-01-01 00:00:01'),
(4, 'a', 'xyz', '' , 'a', '1973-01-01 00:00:01'),
(5, 'a', 'xyz', 'abcity2', '' , '1974-01-01 00:00:01'),
(6, 'b', 'lmn', 'abcity' , 'a', '1974-01-01 00:00:01'),
(7, 'b', 'xyz', '' , 'a', '1975-01-01 00:00:01'),
(8, 'b', 'sdf', 'abcity1', 'b', '1976-01-01 00:00:01'),
(9, 'b', '' , '' , 'a', '1977-01-01 00:00:01'),
(10, 'b', '' , 'abcity2', '' , '1978-01-01 00:00:01');
I want to query that would give me:
'a', 'xyz', 'abcity2', 'a'
'b', 'sdf', 'abcity2', 'b'
I don't want to use a query where i find empty values for each column of the table individually and then take a join as this would be a very cumbersome task given that my actual table has 100 columns.
I have searched for a solution for the past of couple of ours and found nothing. ANy help would be greatly appreciated.
Thanks
This might be the "tricky" way you are looking for.
First create a twin table (tester2) to receive the aggregated data. This new table must have a primary key on pub_name and all the columns you want to aggregate. Then do an INSERT INTO ON DUPLICATE KEY UPDATE. This query will basically rebuild the tester table but without duplicate and with aggregated data. In fact something like this :
insert into tester2 (pub_name, pub_city, country, no_of_branch)
select pub_name, pub_city, country, no_of_branch FROM tester order by estd desc
on duplicate key
update
pub_city = coalesce(tester2.pub_city,tester.pub_city),
country = coalesce(tester2.country,tester.country),
no_of_branch = coalesce(tester2.no_of_branch,tester.no_of_branch)
The content of tester2 will be :
PUB_NAME PUB_CITY COUNTRY NO_OF_BRANCH
a xyz abcity2 a
b sdf abcity2 a
Have a look the DB Fiddle.
Note : I assume you mean real NULL values and not empty string like the sample you provided.
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.