I have a query submitting multiple items in table a.
For example:
insert into a values(id,name) (5,'john'),(6,'smith');
Though I also need to select some third value from other table with this id.
For example:
insert into a values(id,name,money) (5,'john',(select money from b where id=5)),(6,'smith',(select money from b where id=6));
The problem with the above is that it's a bit repetitive and also uses sub selects.
I wonder if it's possible to rewrite this using JOIN, (which should also reassure that there is a relation to the table b on that given id, lest it inserts a NULL).
Any ideas?
You're allowed only one SELECT for each INSERT so you need to re-write this to select multiple rows, not insert multiple values at once. Could you create a temporary table with the two sets of values in it and INSERT those with a JOIN?
CREATE TEMPORARY TABLE _tmp_a (id INT PRIMARY KEY, name VARCHAR(255));
INSERT INTO _tmp_a (5, 'john'), (6, 'smith')
INSERT INTO a (id, name, money) SELECT _tmp_a.id, _tmp_a.name, b.money FROM _tmp_a LEFT JOIN b ON b.id=_tmp_a.id
Related
I wrote a query to display certain record, but it is displaying extra data, for instance I have only 239 records in my database, but the query is displaying 356 records. Can anyone advice me on what I did wrong, I would really appreciate it. Here is the query:
SELECT DISTINCT
t.branchid,
t.occupancyid,
t.wardnumber,
t.bednumber,
t.admissiondate,
ti.patientname
FROM
bedoccupancydetail t
JOIN
consultationheader ti ON t.occupancyid = ti.occupancyid
WHERE
t.checkedout = '0'
There might not be any problem with your query, just it is how mysql (or any RDBMS) behaves. In your case in the two tables bedoccupancydetail and consultationheader are joined by occupancyid and it seems this columns is not unique and contains duplicate values, for each matching (duplicate) record it adds a row/record after joining.
Let's see the below example which I run at https://www.tutorialspoint.com/execute_sql_online.php:
BEGIN TRANSACTION;
CREATE TABLE NAMES(Id integer PRIMARY KEY, Name text);
INSERT INTO NAMES VALUES(1,'Tom');
INSERT INTO NAMES VALUES(2,'Lucy');
INSERT INTO NAMES VALUES(3,'TOM');
INSERT INTO NAMES VALUES(4,'TOM');
CREATE TABLE ABC(Id integer PRIMARY KEY, Name text, Another text);
INSERT INTO ABC VALUES(1,'Tom', 'A');
INSERT INTO ABC VALUES(2,'Lucy', 'B');
INSERT INTO ABC VALUES(3,'TOM', 'C');
INSERT INTO ABC VALUES(4,'TOM', 'D');
COMMIT;
/* Display all the records from the table */
SELECT ABC.Name, NAMES.Name, ABC.Another
FROM NAMES
JOIN ABC on ABC.Name = NAMES.Name;
As you see each table has 4 rows but the result has 6 rows:
$sqlite3 database.sdb < main.sql
Tom|Tom|A
Lucy|Lucy|B
TOM|TOM|C
TOM|TOM|D
TOM|TOM|C
TOM|TOM|D
I have two tables named tbl_A and tbl_B respectively. I'd like to insert a record into tbl_B on existance of the record in tbl_A. Is there single sql statement for that? I think INSERT INTO .. ON DUPLICATE KEY UPDATE is not the one I want.
You could achieve that with WHERE EXISTS. Here is an example but what you want may be a little more involving but yes you can do that.
INSERT INTO contacts
(id, name)
SELECT supp_id, supp_name
FROM suppliers
WHERE EXISTS (SELECT *
FROM orders
WHERE suppliers.supp_id = orders.supp_id);
I am trying to insert data from one table to another in mysql, so would like to understand the below query
insert into department_new select a.*,null from departments a;
what does select a.* means and how does it insert the values correctly into a new table,
kindly help
a is an alias for table departments
and a.* means all column so it is the same as departments.*
So the complete Statement
insert into department_new select a.*,null from departments a;
means, that all values from table departments should be stored into the table department_new plus a new column where null should be inserted.
It only works fine, if the order in both tables are equals. In this form there is the risk, that you copy the values to a wrong column.
It means select all columns from table a which is the departments table.
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.
I have two tables, the first has an auto incrementing ID number, I want to use that as custId in the second table.
I am using an insert into the first table with all the basic info, name, address etc. Then in the second table only 3 things, custId, stocknum, and location. How can I write to these two tables kinda of simultaneously since stockNum may have several values, but always attached to one custId. I hope this makes sense even without putting code in here.
You can't insert into multiple tables at the same time. You have two options. You either do two inserts
INSERT INTO table1 (col1, col2) VALUES ('value1',value2);
/* Gets the id of the new row and inserts into the other table */
INSERT INTO table2 (cust_id, stocknum, location) VALUES (LAST_INSERT_ID(), 'value3', 'value4')
Or you can use a post-insert trigger
CREATE TRIGGER table2_auto AFTER INSERT ON `table1`
FOR EACH ROW
BEGIN
INSERT INTO table2 (cust_id, stocknum, location) VALUES (NEW.id, value3, 'value4')
END
Hope this helps.
After inserting in the first table, The identity field or Auto increment field generate an ID
Get this id Refer Here(LAST_INSERT_ID() MySQL)
Then use this id to store value in the other table