There are 3 columns in the table:
id Name Rating
1 John 0.976
2 Mark 0.500
3 Andrew 0.976
4 Jane 1.000
What sql query could be done to create a new id_new column to this table so id_new actually is the column that has the ordered by Rating?
So, the new table would be:
new_id id Name Rating
1 4 Jane 1.000
2 3 Andrew 0.976
3 1 John 0.976
4 2 Mark 0.500
You see this table looks like it was sorted desc by Rating.
How to do that using mySql query?
Thank you.
If you want to modify the structure of your table and add the new column:
ALTER TABLE `table_name` ADD `new_id` INT( 11 ) NOT NULL FIRST;
SET #new_id:=0;
UPDATE
table_name
SET
new_id = (#new_id := #new_id + 1)
ORDER BY
rating DESC;
If you don't want to modify the structure and just return a SELECT with the new column:
SET #new_id:=0;
SELECT
(#new_id := #new_id + 1) AS new_id,
id,
name,
rating
FROM
table_name
ORDER BY
rating DESC;
ALTER TABLE_NAME ADD new_id int(6) FIRST;
Related
I have a table:---
id
name
dept
1
Alice
CS
2
Bob
Elect
3
David
Mech.
and a query result:-
id
count
1
100
2
22
3
50
Then I want to add the count column from the query to my original table, something like:-
id
name
dept
count
1
Alice
CSE
100
2
Bob
Elect
22
3
David
Mech.
50
The only I figured out to do, is by storing the query result into a new table and then using UPDATE...SET...WHERE. Is there any way to do it without creating a new table?
First you need to create the count column in tablename using
ALTER TABLE `tablename` ADD COLUMN `nr_count` INT;
Then use:
update tablename t
inner join ( SELECT id,
count(*) as nr_count
FROM tablename
GROUP BY id
) as t1
set t.nr_count=t1.nr_count ;
I have multiple queries like
query1=select StudentName from student limit 1;
query2=select StudentAge from student limit 2;
I want something like
category value
StudentName query1 result
StudentAge query2 result
Just to illustrate how far away from a good question you are here's an answer which fully complies with your question but may be absolute rubbish.
drop table if exists student;
drop table if exists t;
create table student(id int, studentname varchar(3), studentage int);
create table t(val varchar(4));
insert into student values
(1,'aaa',19),(2,'bbb',19),(3,'ccc',30),(5,'ddd',20);
insert into t
select * from
(
select StudentName from student limit 1
) a
union all
(
select StudentAge from student limit 2
) ;
select * from t;
+------+
| val |
+------+
| aaa |
| 19 |
| 19 |
+------+
3 rows in set (0.001 sec)
So I have a student_profiles table and ranks table, I want to get the next rank based on the student rank. For example, I have rank 5 then the next rank will be rank 6. So this is my rank structure.
RANKS TABLE:
SELECT * FROM RANKS WHERE style_id = 1"
id style_id level name type primary_colour secondary_colour
1 1 1 Newbie double #4e90b2 #3aad04
22 1 2 Normal solid #fba729 NULL
31 1 3 Expert solid #4e805b NULL
and this is STUDENT_PROFILES TABLE
id | student_id | rank_id
------------------------------------
1 | 1 | 36
2 | 4 | 22
3 | 7 | 10
so all I have a variable is student_id, rank_id & style_id
so for example, I have this value student_id = 4, rank_id = 22 & style_id = 1
It should return
id style_id level name type primary_colour secondary_colour
31 | 1 | 3 | Expert | Solid | #4e805b | NULL
If you just want to get the second row:
Do it like this:
select * from
(select * from table order by id asc limit 2) as a order by id desc limit 1
Any query structure it will work as you need second row if you follow that script.
Try with that:
SELECT * FROM `ranks` WHERE `level` > (SELECT `level` FROM `ranks` WHERE `id` = rank_id) LIMIT 1
But I think it isn't very effective solution.
One option for getting the next highest level in the RANKS table is to self-join this table on the level column, order ascending, and retain the very first record only.
SELECT r2.*
FROM RANKS r1
INNER JOIN
STUDENT_PROFILES s1
ON r1.id = s1.rank_id
INNER JOIN
RANKS r2
ON r2.level > r1.level
ORDER BY r2.level
LIMIT 1
Demo here:
SQLFiddle
Note: If RANKS has duplicate levels, and you want the next level with regard to cardinality (i.e. you don't want a duplicate equal level returned), then my query could be slightly modified to filter out such duplicates.
I have a table named Users.
I import some users from other table.
they have a parent_id
my table i now
id,parent_id,imported_rows_id
1,1,NULL ->my old data has not last row value
2,1,Null ->my old data has not last row value
3,1,1100
4,1100,1101
5,1100,1102
6,1102,1103
Now i want to change all parent_id to id where imported_rows_id = parent_id
same as here:
3,1,1100
4,3,1101
5,3,1102
6,5,1103
update users set parent_id = (select id from users where parent_id=imported_rows_id)
Not allow on the same table
Sincerely
You can do it with a self join:
update TableName t1 join
TableName t2 on t1.imported_rows_id=t2.parent_id
set t2.parent_id=t1.id
where t2.imported_rows_id is not null
Result:
id parent_id imported_rows_id
--------------------------------
1 1 (null)
2 1 (null)
3 1 1100
4 3 1101
5 3 1102
6 5 1103
Result in SQL Fiddle
I have a table which i have 3 columns where first and last are dates.So what i want that i will interchange the 1st column date with the last one only for the first row.Other rows will be as usual.Dates will be dynamic
Table
MyTable
DateAdded1 Name DateAdded2
2015-01-23 A 2015-03-12
2015-02-13 B 2013-03-19
2015-04-23 C 2015-03-12
Now my requirement is that i will interchange the 2015-03-12 date in the place of 2015-01-23 other rows will same .Please someone help me.
you can do it with one query:
update tbl
join
(select DateAdded2,
name,
DateAdded1
from tbl limit 1)q
on tbl.name=q.name
set tbl.DateAdded1=q.DateAdded2,
tbl.DateAdded2=q.DateAdded1;
You can check it.
It may helps you
update mytable
set dateadded1 =(select dateadded2 from mytable where Name ='A'
, dateadded2 =(select dateadded1 from mytable where Name ='A')
where Name ='A'