Mysql merge w tables with common column - mysql

I have:
table1:
Id | Name
table2:
Id | Amount
I want create a new table based on the common Id. So if a record from table1 and table2 have matching id, then:
table3
Id | name | Amount
Sorry if this has been asked before. I'm new to this and just want to get this done

Why wouldn't you do this with a simple select statement?
SELECT a.id, a.Name, b.Amount
FROM table1 a, table2 b
WHERE a.id = b.id

Try using a JOIN
SELECT table1.id, table1.name, table2.amount
FROM table1
LEFT JOIN table2
ON table1.id=table2.id;
I didn't test this but I think it should work.
http://www.w3schools.com/sql/sql_join_left.asp

Like this :
CREATE TABLE tavle_xxx(
id xxx,
name xxx,
amount xxx
);
INSERT IGNORE INTO tavle_xxx
SELECT t1.id, t1.name, t2.Amount
FROM table1 t1, table2 t2
WHERE t1.id=t2.id;

update:
I'd like to merge the tables based on the common column "id" and NOT create a 3rd table. Just add the "name column" and populate it where the id's are =
so i have:
table1:
Id | Name
table2:
Id | Amount
I would like the result to be
table2
Id | Amount | Name

If you want to merge the Name column of table1 in table2, using the common column Id, you can do this by running:
ALTER TABLE table1 ADD Name <dataTypeOfName>;
UPDATE table1, table2 SET table2.Name = table1.Name WHERE table1.Id = table2.Id;
This will create a new column called Name in table2 and then fill it with the values from table1, after performing a join based on the column Id.

Related

How do I search relations on tables SQL

I have 3 tables like this
With the tables filled like this:
How do I search the cases In where on the table 3 the idtable 1 has all the id from table 2 related?
For example idtable1 = 1 would be an output of that query cuz is related with every id from idtable2
Presumably, you intend:
select t1.*
from table1 t1
where (select count(*) from table3 t3 where t3.idtable1 = t1.idtable1) =
(select count(*) from table2);
This shows all records from table1 where table3 contains all values of idtable2 -- assuming no duplicates in table3 (and that the ids are unique).

How do I include table names in a MySQL query?

This seems like it would be a simple setting, but I cannot find it. I do inner join queries on tables that have similar column names. It would be nice to include the table name in the query results, so the people receiving the data can differentiate more easily. For example:
Table1:
id
name
timestamp
Table2:
id
name
timestamp
table1_id
Table3:
id
name
timestamp
table2_id
Then I tie it all together with a query:
select * from table1
inner join table2 on table1.id=table2.table1_id
inner join on table2.id=table3.table2_id;
The results have similar column header names:
id name timestamp id name timestamp table1_id id name timestamp table2_id
It's hard to tell the data apart. Of course the example query is short and silly and pointless. If I do an actual query with all the data it get more complicated. Couldn't the column header name include the table name?
table1.id table1.name table1.timestamp table2.id table2.name table2.timestamp table2.table1_id table3.id table3.name table3.timestamp table3.table2_id
You have ambiguous column names in output: table1.id, table2.id
Adding alias for columns should solve this:
SELECT table1.id as t1_id, table2.id as t2_id
Instead of writing
select * from
you can write
select table1.id as table1_id,
and do the same for the other columns so that the results set would show you the names you give yourself for each column
You can use aliases to identify columns:
SELECT table1.id AS table1_id FROM ...
But you would have to do this for each field you want to select.
try this.hope it will help you.
SELECT table1.id as t1_id, table2.table1_id as t2_id
FROM tablename
inner join table2 on table1.id=table2.table1_id
inner join table3 on table2.id=table3.table2_id;

SELECT * FROM table1 LEFT JOIN table1.value AS table2

i have a db like this:
Table1:
id
id_item
table (enum: 'table2','table3','table4')
table2:
id
value
table3:
id
value
table4:
[...]
And i want to run a query like this:
SELECT t1.id, t2.value FROM table1 AS t1
LEFT JOIN table1.table as t2 ON t1.id_item=t2.id
Is it possible? or i have to select first table1 and after the value?
( sorry for my bad eng :) )
If I understood the last column in Table 1 correctly and it is just a string, you can't.
You cannot write a column into the FORM-clausal and wait for mysql to evaluate it for every row and find the correct table to join it with.
To do this you will need to create a view where you will have the data from all the tables, together with the table name as an additional column. Afterwards you can perform a join like that between Table1 and the new view

Merge structurally different MySQL tables on a common value

I am trying to join 2 tables to a new table based on a shared value, but not having much luck.
Here's what I've got.
Table1:
id |name |
----------|---------|
1 |test1 |
Table2:
id |name |location |color |
----------|---------|---------|---------|
1 | test1 | 10 | blue |
What I'm after here is a new table (Table3) that takes "name" from Table1 and matches it up against name from Table2 and then sticks the matching results into Table3. Anything that doesn't match table 1 should be ignored. So if I had "test99" in Table2, but not in Table1, don't put it in Table3.
Everything I've read says this shouldn't be hard to do, but I'm just not having any luck with it.
Thank you!
Try
SELECT Table2.Name, Table2.Location, Table2.Colour
FROM Table2
INNER JOIN Table1 ON Table2.Name = Table1.Name
Although I noticed you have Id which might be assumed is your Primary Key and Foreign Key ? If so try
SELECT Table2.Name, Table2.Location, Table2.Colour
FROM Table2
INNER JOIN Table1 ON Table2.Id= Table1.Id
You could do a select into table.
Select * INTO Table3
FROM Table2
WHERE Table2.Name IN (SELECT Name FROM Table1)
Or you could use INSERT ... SELECT -syntax and use JOIN.
INSERT INTO table3 (id, name, location, color)
SELECT t.id, t2.name, t.location, t.color FROM table1 AS t JOIN table2 AS t2 ON t2.id = t1.id

MYSQLI SQL query over multiple tables fail

I am very new to SQL and i need to retreive a number of attributes from different tables but. I am searching for images and details about a user by searching for them under their name.
For example:
SELECT * FROM table1 WHERE name LIKE '%tim%' AND last_name '%smith%'
I need the following attributes in my result:
I will need all the details from table 1
account id and image from table 2
path from table 4
I have tried
SELECT table1.id table1.name table1.last_name table 2.account_id table4.path FROM table1 table 2 table4 WHERE table4.image_id = table 2.image AND table1.first_name LIKE '%tim%' AND table1.last_name LIKE '%smith%'
I am considering spilting this up into multiple queries which would solve this issue, however I am sure this can be done under a single query?, I am just not sure how to structre it, I have tried a number of times, but all queries fail in phpmyadmin
Hope someone can point me in the right direction? Below are an example of my tables, thanks in advance!
table1
------------
id (pk)
name
last_name
table 2
--------------
account id (pk)
id (fk)
image (holds image id from table 4)
table 3
-----------------
account id (fk)
image id (fk)
date
table 4
--------------------
image id (pk)
path
date
Try with this:
SELECT t1.*, t2.account_id, t4.path
FROM table1 t1
INNER JOIN table2 t2 ON t2.id = t1.id
INNER JOIN table4 t4 ON t4.image_id = t2.image
So you have INNER JOINs between table1 & table2, and between table2 & table4. In MySQL, JOIN implicitly means INNER JOIN.
SELECT
table1.id,
table1.name,
table1.last_name,
table2.account_id,
table4.path
FROM
table1
JOIN table2 ON table1.id = table2.id
JOIN table4 ON table2.image = table4.image_id
WHERE table1.name LIKE '%tim%' AND table1.last_name LIKE '%smith%'
(Edit: Forgot to add in the original WHERE clause.)