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
Related
I have two databases named drupal and wordpress. I try to migrate post image paths from drupal 6 to wp.
Tables drupal.content_field_image and drupal_files contain necessary data:
drupal.content_field_image has field_image_fid - nid pair. drupal.drupal_files has fid - filepath pair (field_image_fid = fid).
I need to get the table that contains both nid and filepath, so I join this tables:
SELECT *
FROM `content_field_image`
JOIN `files` ON content_field_image.field_image_fid = files.fid;
Now I need to insert data to wordpress db so that:
meta_id = 34 + n (n is increment)
post_id = nid from joined table
meta_key = fifu_image_url
meta_value = filepath from joined table
So I have some questions:
How to make insert from joined table?
How to make while-like loop to insert every entry from joined table?
How to make n increment by 1 after every insert?
How to make insert from joined table?
Use a INSERT INTO .. SELECT FROM construct
How to make n increment by 1 after every insert?
Declare that column n as auto_increment column. Else, you will have to do it yourself if your concerned table already has a auto_increment column in place.
How to make while-like loop to insert every entry from joined table
You don't need that at all. INSERT .. SELECT construct will insert all the fetched rows to your referred table.
Your insert with select could be something like this:
insert into wordpress.table
SELECT (#i:=#i+1), nid, fifu_image_url, filepath
FROM drupal.content_field_image
JOIN drupal.files ON content_field_image.field_image_fid = files.fid;
join (select #i:=34) inc on true
To make it work, the columns of your select must have the same columns of the table you are inserting.
The incremental int could be made with this variable #i, initialized as 34 like you wanted and incremented 1 by 1 as the results as printed.
i got the following insert-command:
INSERT INTO PERSON ('Name','Age','Filename') VALUES ('Max',12,'Max_ID_.pdf');
i want to insert instead of 'Max_ID_.pdf' the string concartenated with the inserted id for this row. e.g.:
ID|Name|Age|Filename
2 |Max |12 |Max_2_.pdf
You can insert your row first, and than update it with the last inserted id:
INSERT INTO PERSON ('Name','Age','Filename') VALUES ('Max',12,'xxx');
UPDATE PERSON Filename=CONCAT(LAST_INSERT_ID(),'.pdf') where id = LAST_INSERT_ID()
There is also a way to do it in one statement, maybe a little more complex, and maybe will not work on every system, e.g. if you use innodb or transactions:
INSERT INTO PERSON SET Filename = CONCAT((SELECT auto_increment FROM
information_schema.tables WHERE table_name='PERSON'), '.pdf'),
Name = 'Max', Age = '12'
Given this tables structure,
categories{id}
product_models{id, category_id}
products{product_model_id, category_id}
Which is already populated, but with product_models.category_id, products.category_id and products.product_model_id all set to NULL, I need a single query that will "connect" them all, doing the follow:
set all product_models.category_id, to a random value from the categories table
set all products.product_model_id, to a random value from product_models table
set each products.category_id, to the value of the category_id of the newly assigned product_models record.
Can it be done in a SINGLE query?
If i am able to understand you requirement, this is what you require
Create Procedure usp_insert
as
begin
declare #rand1 int
declare #rand2 int
set #rand1=rand()*10000
set #rand2=rand()*10000
insert into categories (id) values (#rand1)
insert into product_models{id, category_id} values (#rand2,#rand1)
insert into products{product_model_id, category_id} values (#rand2,#rand1)
End
above block will create a procedure in your database
to execute the procedure use following code
exec usp_insert
Each execution of the procedure will insert one row in each of the tables
e.g.
suppose random numbers generated are 3423,2345
then it will
1. insert a row in categories table with 3423 as id
2. insert a row in product_models table with 3423 as category_id and 2345 as id
3. insert a row in product_models table with 3423 as category_id and 2345 as product_model_id
you can adjust the insert queries according to your requirement.
No
The RAND function only executes once in any query and is effectively 'locked' to a single value no matter how many times it is used.
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
Hi I would like to copy entire contents from column Item under table IName to column Name under table Item both belonging to the same database.
I am giving the following query but it throws the error saying that the subquery returned more than one records. (There are around 600 records)
Insert into Item set name = (Select Item from IName)
Thanks
INSERT INTO Item (Name)
SELECT Item
FROM IName
When you want to insert into a single-column* table, INSERT works either with:
INSERT INTO table (column)
VALUES (value1),(value2), ... (valueN) ;
or with:
INSERT INTO table (column)
SELECT a_column
FROM a_table
--- optional (multiple) JOINs
--- and WHERE
--- and GROUP BY
--- any complex SELECT query
(OK, the above can work with a multiple-column table, too, as long as all the other - not explicitely stated in the INSERT statement - columns have been defined with a DEFAULT value or with AUTO_INCREMENT.)
The INSERT ... SET syntax is valid in MySQL only and can be used only when you want to insert one row exactly:
INSERT INTO table
SET column = value1 ;
is equivalent to:
INSERT INTO table (column)
VALUES (value1) ;
INSERT INTO Item (name)
SELECT Item FROM IName
Link
INSERT INTO table_one (column1) SELECT column2 FROM table_two
See MySQL Ref