How to rearrange the column position in MySQL [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can I change the column position to first using ALTER query in MySQL table?

If you want your column at first position, you should use
ALTER TABLE tableName MODIFY COLUMN yourColumnName varchar(50) FIRST
For example, if your table is
Employee{FirstName,LastName,EmpId}
And if you want to move EmpId to first position
ALTER TABLE Employee MODIFY COLUMN EmpId varchar(15) FIRST;

For example :
Below query will bring empname column after department :
ALTER TABLE Employees MODIFY COLUMN empName VARCHAR(50) AFTER department;

Related

SQL Query For Values From Another Table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a team_name column but I want to instead store the team_IDs of the same teams from column (team_ID) in a different table to use as a foreign key and then drop team_names. Here are some screenshots
The table I have (players):
The table I want to get the values of team_ID from (teams_in_league):
For each row where team_ID is null, I want that to be the ID of the team_name contained in the next column. What query can I use to achieve this? Team_ID is meant to be a foreign key btw
EDIT:
Extra info: there should be two tables, I'm going to add more columns to the second table. That's why I'm not joining the two tables.
You need to join the tables in an UPDATE statement:
UPDATE players p
INNER JOIN teams_in_league t
ON t.team_name = p.team_name
SET p.team_id = t.team_id
After you do the update, you can remove the column team_name from the table players because it is redundant:
ALTER TABLE players
DROP COLUMN team_name;
and make the column team_id of players a foreign key that references team_id of teams_in_league:
ALTER TABLE players
ADD FOREIGN KEY (team_id) REFERENCES teams_in_league(team_id);

ERROR 1136 (21S01) when INSERT data in MySQL [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
A MySQL table called SQ was created as follows,
CREATE TABLE SQ(
SQno INT PRIMARY KEY,
Question VARCHAR(100)
);
After I was trying to enter data to that table like this,
INSERT INTO SQ (SQno, Question) VALUES(
(1,'What primary school did you attend?'),
(2,'In what town or city did your parents meet?'),
(3,'In what city or town was your first job?'),
);
But there was an error occurs, and shows as this,
ERROR 1136 (21S01): Column count doesn't match value count at row 1
So, please help me to resolve this error?
You need to get rid of the outer set of parentheses; each row should have parentheses around it but not also all the rows:
INSERT INTO SQ (SQno, Question) VALUES
(1,'What primary school did you attend?'),
(2,'In what town or city did your parents meet?'),
(3,'In what city or town was your first job?')
;

SQL query for price [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have table with columns date and price
Can I find rows where the price was more than X percent from the previous price?
My table is
CREATE TABLE IF NOT EXISTS `mydb`.`prices` (
`id` INT NOT NULL,
`date` DATE NULL,
`price` FLOAT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
you want the lead() and lag() functions (one looks to the row above, and the other to the row below). (Here's an example)
For instance:
select * from (
select [date], [price], lag([price],1,0) over (order by date) as lagged_price
from your_table
) sub
where lagged_price / price > X -- here x would be 1.2 if you want it to be at least 20% greater

Select values which are not in other table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Let's say i have two tables.
Table client
ID - primary key ( 1,2,3,4,5,6)
Table Orders
OrderID - primary key
ClientID (1,2,4,5)
I need to get ROWS of table CLIENT of clients 3 and 6 (which don't have orders / are not in orders table)
This is basic query. Try your self.
select * from client where id not in (select ClientID from orders)

Insert a value in a column seperate by comma [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Insert a value in a column name e-mail then it display all emails seperated by comma. This email is save at different time but for same id.
Is it possible then how?
You can use GROUP_CONCAT to show the comma separated records that belong to same group,be aware of that fact GROUP_CONCAT has a default limit of 1024 characters to concat but it can be increased as mentioned in docs
CREATE TABLE Table1
(id INT ,`test` varchar(25))
;
INSERT INTO Table1
(id,`test`)
VALUES
(1,'test#test.com'),
(1,'test#test.com'),
(1,'test#test.com'),
(2,'test2#test.com'),
(2,'test2#test.com'),
(2,'test2#test.com')
;
SELECT id, GROUP_CONCAT(test) emails
FROM
Table1 GROUP BY id
Fiddle Demo