I need some help again.
I have to insert two values in a table into "hospital_database". This table has five columns, and it's called "personas". The columns' names are "cod_hospital(PK)", "DNI", "Apellidos", "Funcion" and "Salario"... I have to insert "99887766" and "Martínez Martínez, Alejandro" into "DNI" and "Apellidos", but according to the question, I must to insert into a "hospital" where only there's 1 person...
I have to use "insert+select" and my last effort was this:
insert into personas
values (99887766, 'Martínez Martínez, Alejandro');
select dni, apellidos
from personas
where count(dni)=1;
I tried something like that, and a lot of ways... but It doesn't work like the question asks. I have to use insert+select, so I shouldn't write ";" before "select".
Honestly I'm still guessing at this a little, but maybe you intend to insert an additional row in the personas table if only 1 row exists in that table for a given hospital code. To do this you need to use group by with having:
insert into personas (cod_hospital, dni, apellidos)
select cod_hospital, 99887766, 'Martínez Martínez, Alejandro'
from personas
group by cod_hospital
having count(*)=1
insert have several sintaxis and looks like you are mixing all of them
One is insert values http://www.w3schools.com/sql/sql_insert.asp
INSERT INTO table_name
VALUES (value1,value2,value3,...);
the other insert from a select
http://www.w3schools.com/sql/sql_insert_into_select.asp
INSERT INTO table2
SELECT * FROM table1;
And if you dont include all the field, you have to indicate which fields are you inserting
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
cod_hospital is the table's PK, you could only insert one person per hospital, change your PK to: cod_hospital, dni
Related
I have a many to many relationship and am trying to insert a new relationship. At the point I'm doing the insert I don't know the id so would need to look it up. My table structure looks something like this:
**person**
id
name
**film**
id
title
**person_film**
personId
filmId
Given a person's id and a list of film titles (but not their ids) I'm only able to insert these relationships into the person_film table in two steps.
SELECT id FROM film WHERE title="film1" OR title="film2";
Then the results of that can be used in an insert:
INSERT INTO person_film (personId, filmId) VALUES (5, 5),(5, 7);
Is there a way of doing this in a single SQL statement?
You can do it with a subquery:
INSERT INTO person_film (personId, filmId)
SELECT 5, id FROM film
WHERE title IN("film1","film2");
Here the 5 is the personId value and the filmId will be retrieved from the film table.
Use numeric literals with aliases inside a SELECT statement. No () are necessary around the SELECT component.
INSERT INTO person_film (personId, filmId)
SELECT
/* Literal number values with column aliases */
1 AS fpersonId,
2 AS filmId,
FROM film
WHERE title="film1" OR title="film2";
Note that in context of an INSERT INTO...SELECT, the aliases are not actually necessary and you can just SELECT 1, 2 but in a normal SELECT you'll need the aliases to access the columns returned.
INSERT INTO class
(name, description, personid)
Select name, description, 12 from Class where PersonID = 3;
Select * from Class
Select * from Person
Why is the values words is missing from above statement? I thought it should be like this insert into tableA('name') values('select name from tableB') ?
INSERT INTO my_table VALUES ()
Insert data one Table to another table
OR
Not Using Value keyword
Insert into Table2 (Name , Address , Mobile) Select Column 1, Column 2 , Column 3 From Table1
There are different techniques of INSERT, the code above is inserting values from the table itself and change only the personid to 12, he use select so that he can copy the data aside from hardcoded personid . that's why you didn't see the VALUES keyword , but that's true.. the basic insert statement we learn from school is INSERT INTO TableName (Col1, Col2... etc) VALUES (Value1, Value2... etc) , INSERTION of data depends on the requirements that you are working on.
INSERT INTO numbers (type, number)
VALUES 'telephone', SELECT DISTINCT tel FROM flat_data
I am attempting to select distinct numbers from one table and then insert them into another table.
However I need to manually set the type column that I am inserting into manually.
I can do it without the DISTINCT but I can't get my head around how to do it with!
INSERT INTO numbers (type, number)
SELECT DISTINCT 'telephone', tel
FROM flat_data
I have one table which I have imported into mysql.
Now I need to create multiple related tables.
So basically I have currently got the following
start transaction;
Insert into Address (AddressLine1, AddressLine2, Town, County, Postcode)
Select Distinct Address_line_1, Address_Line_2, Town, County, Postcode from Import;
set addressLastId = last_insert_id();
INSERT INTO Organisation (Name, AddressID)
SELECT DISTINCT Supplier_Name, addressLastId FROM Import;
commit;
The second part where I use the last_insert_id never increments probably because it gets the last insert.
So I need to workout how i can get the previous id for each row inserted into the address table?
Would i need to have an inner loop within the address insert ?
Cheers
I agree with Tim.
After you've populated Address, then you could just run
INSERT INTO Organisation (Name, AddressID)
SELECT DISTINCT Import.Supplier_Name, Address.id
FROM Import INNER JOIN Address ON (set all the address lines and city etc =, since Im guessing there wasnt an address ID in the original import)
You could use a join for the second insert - join Address and Import, and insert the required fields from each into Organisation.
Getting the last insert ID will only work if you process each record sequentially.
I have a table named student which consists of (rollno, name, sem, branch)
If I want to INSERT only one value (i.e only name has to be entered) what is the query?
To insert values into specific columns, you first have to specify which columns you want to populate. The query would look like this:
INSERT INTO your_table_name (your_column_name)
VALUES (the_value);
To insert values into more than one column, separate the column names with a comma and insert the values in the same order you added the column names:
INSERT INTO your_table_name (your_column_name_01, your_column_name_02)
VALUES (the_value_01, the_value_02);
If you are unsure, have a look at W3Schools.com. They usually have explanations with examples.
insert into student(name) values("The name you wan to insert");
Be careful not to forget to insert the primary key.
First, if the table is all empty, just wanna make the column 'name' with values, it is easy to use INSERT INTO. https://www.w3schools.com/sql/sql_insert.asp.
`INSERT INTO TABLE_NAME (COLUMN_NAME) VALUES ("the values")`
Second, if the table is already with some values inside, and only wanna insert some values for one column, use UPDATE. https://www.w3schools.com/sql/sql_update.asp
UPDATE table_name SET column1 = value1 WHERE condition;
insert into student (name)
select 'some name'
or
insert into student (name)
values ('some name')
Following works if other columns accept null or do have default value:
INSERT INTO Student (name) VALUES('Jack');
Further details can be found from the Reference Manual:: 13.2.5 INSERT Syntax.
Execute this query, if you want the rest of columns as "#", do insert # inside the single quote which is left blank in query.
Also, there is no need of defining column name in the query.
insert into student values('','your name','','');
Thanks...