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;
Related
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.
I'm running a query via PDO on a pair of joined tables like so:
SELECT table1.id, table2.id, table1.foo, table1.bar
FROM table1 INNER JOIN table2 ON table1.bar = table2.id;
Both tables have an id column so when I run fetchAll() the associative array only contains one id field. This is because the first is overwritten by the second.
Is there a way to obtain both id fields? Perhaps by having the table name included in the array keys...
use aliases
SELECT table1.id as t1id, table2.id as t2id
--etc.
try this
SELECT table1.id AS idtable1, table2.id AS idtable2, table1.foo, table1.bar
FROM table1 INNER JOIN table2 ON table1.bar = table2.id;
In table1 one of the fields is member_id.
In table2 one of the fields is member_username and the id field in this table is equal to the member_id field in table2.
The goal is to display all results from table1 in ascending alphabetical order based on member_username from table2.
Basically I need to resolve the member_id from table1 to a member_username from table2 and sort them alphabetically.
Any ideas?
You need to use a join from table1 to table2 to pick up the username, then sort on this field. You just need to be wary of a one-to-many relationship, i.e. if a member might have more than one entry in table2 with the same id you may get more rows than you expect.
select *
from table1
left join table2 on table2.id = table1.member_id
order by table2.member_username
If I didn't misunderstand your question, try joining both tables together and sort by member_username.
SELECT t1.*,
t2.member_username
FROM table1 AS t1
INNER JOIN table2 AS t2 ON t1.member_id = t2.id
ORDER BY t2.member_username ASC;
You can leave t2.member_username in the SELECT-part of the query, I just put it there for reference.
I have two table in database.
Table1 -> Name
Table2 -> Name
What will be query to get all the "Name" from Table1 and Table2 into single Column.
This query returns the value from the Name column from Table1 and the Name column from Table2, concatenated together into a single resultset.
SELECT t1.Name FROM Table1 t1
UNION ALL
SELECT t2.Name FROM Table2 t2
(This was my understanding of what you were looking for.)
If you want just a "distinct" list of Name values (exclude duplicate occurrences of the same value), then remove the ALL keyword.
If I correctly understood
http://dev.mysql.com/doc/refman/5.0/en/union.html
Select name from table1
union
Select name from table2
You can select data from two tables like this.
SELECT CONCAT(table1.name,table2.name) as Name FROM table1,table2;
and in case table1.name is A and table2.name is b you get
Name = AB
SELECT Name FROM Table1 NATURAL LEFT JOIN Table2 AS t2.
This will give you a list of only non-duplicate names from Table1 and Table2.
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.)