Add values in SQL [closed] - mysql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a table with two attributes
Staffnumber Name
Now I want to add a new staff member into this table, for example: Christian, and Staffnumber is the current max Staffnumber + 1. (Assume I have til now 20 staff members, then Christian would be number 21)
How could i do that, without knowing how many staff members I had? Is there any way to make SQL numbers the Christian as 21?

Making a call to the table to count the records should achieve sort of what you're looking for. Forgive my syntax but it should be something along these lines...
INSERT INTO TABLE_NAME (STAFF_NUMBER, NAME) VALUES ((SELECT COUNT(*) FROM TABLE_NAME) + 1, "Christian");
Keep in mind if you delete staff members and add more later this won't work because the count will be off.
If you make your STAFF_NUMBER column a primary key, you shouldn't have to specify this value at all when entering new values, it should auto-increment and assign a random ID to the staff member.
INSERT INTO TABLE_NAME (NAME) VALUES ("Christian");

If there is no auto-increment on your column, then you would just do a simple INSERT statement:
INSERT INTO table_name (Staffnumber, Name)
VALUES (21, 'Christian')
Also here is a link to set AUTO_INCREMENT for your column for MySQL:
http://www.w3schools.com/sql/sql_autoincrement.asp

Increment
So if you don't have auto increment. You will want to select the COUNT() from Staffnumber.
SELECT COUNT(*) + 1 From Table;
Inserting
Now you also want to insert a new record into the table.
INSERT INTO Customers VALUES(1,"Christian");
But the problem with this is that it will only Staffnumber of 1. So you need to combine both of these.
Final
INSERT INTO Table VALUES((SELECT COUNT(*)+1 FROM Table),"Christian");
INSERT INTO Table VALUES((SELECT COUNT(*) FROM Table) + 1,"Christian");

Related

How do I join two tables, then insert into a third table with a NEW primary key? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Here is the statement I have now:
INSERT INTO jobs
SELECT *
FROM proposals
JOIN commissions
ON proposals.commission_id = commissions.job_id
WHERE proposals.proposal_id = '123'
It's working fine but is creating some primary key conflicts. So, what I would like to do when the new row is entered, is this:
new primary key + commissions table data + proposals table data
Any ideas?
then if you have auto increment pk , do this :
INSERT INTO jobs ( [list of columns except the auto increment column])
SELECT ( [list of columns accordingly])
FROM proposals
JOIN commissions
ON proposals.commission_id = commissions.job_id
WHERE proposals.proposal_id = '123'
Like p.Salmon said in the comments, I would add a new column to the jobs table
ALTER TABLE Jobs add autoid INT(10)AUTO_INCREMENT PRIMARY KEY;

How to select from table A only where a relationship between the table A row and a specific row in B exists? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
lets say I have two MySQL tables, table A and table B. Each table has a primary key called id and a name column. I also have a third table, table C, that contains relationships between table A and table B. Each row contains two foreign keys called a_id and b_id, which, as you might expect, correspond to ids in tables A and B.
What I want to do is select a random set of 10 table A rows, but only select rows that have a relationship with specific entries in table B. I don't know which entries I'm looking for ahead of time, and I will start with their names. The names will be provided via query parameters.
I understand I should probably start with this:
SELECT * FROM `A`
ORDER BY RAND()
LIMIT 10
But I don't know how to structure the where clause.
You need something like this:
SELECT *
FROM `A` a
INNER JOIN `C` c ON
a.ID = c.a_id AND
c.b_id in (1,2,3,4) -- your entries here
-- order and limit as you wish
ORDER BY RAND()
LIMIT 10

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

SQL Remove Row That Has The Same Values Into New Table [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove Duplicate Rows Leaving Oldest Row Only?
MySQL remove duplicate rows
Say that I have the following table coolumns: int logicalid(pk) ,int home, int person say the I have the following records...
1,5,6
2,5,6
3,5,5
4,5,5
After my query, I want to place into the new table only one row with the same home,person column values so this will be the output result:
1,5,6
2,5,5
Any ideas??
Create your new table with an auto-increment column for the id.
Then insert into the table, using a query such as:
insert into newTable(home, person)
select distinct home, person
from oldTable
INSERT INTO newtable(home, person) SELECT DISTINCT home, person FROM sourcetable

update if exists [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I update if exists, insert if not (aka upsert or merge) in MySQL?
I have a table with the columns user, category and score. I want to run an insert that, if this user/category combination already exists instead just updates the score. Both the user and the category column can have the same value many times but each specific user/category combination may only exist on one single row. How can I do this?
Many thanks
For this example, let's call your table scores:
Make sure you have a unique index
ALTER TABLE scores ADD UNIQUE INDEX user_category_ndx (user,category);
Now run this to replace:
INSERT INTO scores (user,category,score) VALUES ('username','mycat',127)
ON DUPLICATE KEY UPDATE score = VALUES(score);
If your updating the score means add up scores, then do this:
INSERT INTO scores (user,category,score) VALUES ('username','mycat',127)
ON DUPLICATE KEY UPDATE score = score + VALUES(score);
Give it a try!
UPDATE 2011-08-28 09:00 EDT
If you want to keep just high scores and then add the unique index, here is what you must do:
CREATE TABLE scores_new LIKE scores;
ALTER TABLE scores_new ADD UNIQUE INDEX user_category_ndx (user,category);
INSERT INTO scores_new (user,category,score)
SELECT user,category,MAX(score) FROM scores GROUP BY user,category;
ALTER TABLE scores RENAME scores_old;
ALTER TABLE scores_new RENAME scores;
#
# Drop old table if no longer needed
#
DROP TABLE scores_old;
UPDATE 2011-08-28 10:53 EDT
Run this to keep maximum score:
INSERT INTO scores (user,category,score) VALUES ('username','mycat',127)
ON DUPLICATE KEY UPDATE score = GREATEST(VALUES(score),score);
What you want is INSERT ... ON DUPLICATE KEY UPDATE. Make sure you have a unique index defined on user, category. See http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html