Merge columns to create new records - mysql query - mysql

I have a mysql database and I need to merge two columns together, however I need to merge the columns in such a way that they are not just concatenated together but instead a whole new separate record.
Example:
Before Merge:
name Computer-one Computer-two
jack hp dell
<insert unkown sql statement (what I need)>
After Merge
name Computer-one
jack hp
jack dell
When I try to merge the two columns they simply merge in to a single column but do not repeat the record instead (look above):
name Computer-one
jack hpdell
Currently I am using the concat statement. Which is the only way I've been able to get the items to merge. These columns can contain null values, there is a ID that auto increments for the primary key.

The problem is that you actually dont want to merge them.
You want to create new rows for a field.
You can just do this.
INSERT INTO table_name(name, Computer-one)
SELECT
name, Computer-two
FROM
table_name;
Then you will create new rows for all rows with there Computer-two values put into the Computer-one field.
When you see that your table has twice as many rows and the Computer-one field is correct you can just drop the 'Computer-two' column and the data is as you wanted.
Always make a backup first or try on test-tables so that you know that everything works first.

I think you just need UNION ALL statement. You may try below -
SELECT name, "Computer-one"
FROM YOUR_TABLE
WHERE "Computer-one" IS NOT NULL
UNION ALL
SELECT name, "Computer-two"
FROM YOUR_TABLE
WHERE "Computer-two" IS NOT NULL;

So you need to do two queries on this table and union the result. Insert results into temp table. Then select distinct records for each record for column 'nane'
SELECT INTO table2(name, model)
SELECT A.name, A.computer-one AS model from table as A
UNION
SELECT B.name, B.computer-two AS model from table as B

Related

How can I control table name when multiple tables are selected?

I'm using MySQL and I selected three tables in one statement like that:
SELECT * FROM tb_i, tb_s, tb_t
But I need case control by their table names. If a row from tb_s it returns tb_s on additional column table_name.
How can I get table name from this statement?
The way you add up your tables now is a JOIN - meaning that every row is a Cartesian product of three of them - so each row is returned from all the three tables.
If you would like to concatenate the data from the three tables you should use UNION ALL which assumes that the columns are of the same structure. Then you would able to mark the origin table, with an addition constant field.
For instance in your case:
SELECT tb_i.*,'tb_i' as source
FROM tb_i
UNION ALL
SELECT tb_s.*,'tb_s'
FROM tb_s
UNION ALL
SELECT tb_t.*,'tb_t'
FROM tb_t
where the source column is a constant string per each table.

How do I combine data from 3 different Access tables into one master table?

I have 3 access tables with information from the past 3 years. There are tons of the same records in each but there are also unique records in each.
2 tables have the same unique primary key (ID) while the 3rd table has a different set of unique IDs
How do I combine and select all the unique ID's into one master table? Thanks
Not 100% sure I understand where the overlaps occur and not, but try this:
select ID
into All_Id
from (
select ID from Table1
union
select ID from Table2
union all
select ID from Table3
)
This presupposes that Table1 and Table2 might share some IDs, and you only want them listed once, but Table3 doesn't have any overlaps.
Truth be told, there is no harm in making them all union, other than maybe having the query run slower.
If you want unique IDs, use a UNION query. If you want everything, use a UNION ALL.
UNION = no dupes
UNION ALL = returns all records including dupes
The Access engine supports union queries but you have to manually write the union query in the SQL view. Design view is not available.
Depending on how much data you have from the past three years, the UNION might take some time and may even blow up a few times. I'd make a back up copy first just in case.
If you want purely unique IDs and a new table, here's what I would do:
1.) Write your union query.
SELECT ID FROM Table1
UNION
SELECT ID FROM Table2
...
2.) Save the query.
3.) Create a make table query (to select and combine all unique IDs into a presumably new master table).
4.) Run the make table query. The new table will be created.
Hope that helps. Let us know how you make out!

MySQL SELECT Statement query join 2 tables

I have a problem when I write query select statement I have a lot of row duplicated. My table like this:
tbl_injection
tbl_patient
My Expect result is wanted to return a row because my table tbl_patient has only 1 row. And I just want to get the injection_status from tbl_injection.
Seems like patient_id is not unique enough. Try to put more filter such as injection_date maybe?
Or you can return the result of "latest" injection_status instead.
As you see, patient_id value 1 of tbl_patient is recorded 16 times in tbl_injection table, So you can not one low by using patient_id column only.
If you don't want to change those table records, you have to make a query additional columns, such as injection_data which has unique value.

mySQL select columns from different tables

1000 Apologies if I've repeated a question, couldn't find an answer here to my question.
I'm try to retrieve the data from 2 separate columns from 2 unrelated tables in the same query.
I've tried using a UNION statement, but the problem is that I need to be able to separate the results into 'venues' and 'programmes' - here was what I did:
SELECT venue_name
FROM my_venues
UNION
SELECT programme_title
FROM my_programmes;
Maybe it's not necessary to combine the query and I can just do 2 separate queries? The database won't be especially large, but it seems unnecessary...
Help and thanks!
Just add a constant column in both selects, with the same name, but different values:
SELECT "venues" as source, venue_name as thing_name
FROM my_venues
UNION ALL
SELECT "programmes" as source, programme_title as thing_name
FROM my_programmes;
Now:
Rows with value "venues" for column
source will come from the table
my_venues ,
rows with value "programmes" for
column source will come from table
my_programmes.

Search SQL query from multiple tables MySQL

I am trying to perform search on multiple tables.
I will simplify problem and say that I have 2 tables Worker and Customer both have Id, Name, Surname and Worker has additional Position, all fields are varchar except Id which is Int.
How to make a query that will return rows of either Customer or Worker, where one of theirs fields contains entered search string.
I have tried with joins but I got returned joined row also.
select id,name,surname,position,'worker' as tbl from worker where ..
union all
select id,name,surname,'','customer' from customer where ...
In this way you can even know results what table belong to.
Just UNION both queries.
If you really can JOIN those two, you can use
an IF statement in the SELECT clause to show the right field.
But, from what I understand from your question, go with UNION