I have been looking and trying to join two tables to get one result but with no luck. (yes a newbie)
Here is the scenario:
Table T1
ID | t1_name | **t1_value**
Table T2
ID | t2_something | **t2_name** | t2_more | t2_etc
What I am trying to do is a SELECT query to get a result to ECHO the t1_value.
This is from t2_name equaling the same as t1_name then return the t1_value to ECHO.
These are based on each indivdual "t2 ID" (100 + ids) having its own result.
Im sure this is an easy solution, and all the reading and research I have done on joins i just cant seem to get my head around it. Maybe over reading....
If you can offer a solution/help, (with how it works so I can learn for next time)
That would be appreciated.
JOIN the two tables with the condition t1.t2_name = t2.t2_name, then in the SELECT clause select whatever you want to select.
Something like:
SELECT
t2.ID,
t1.t1_value,
...
FROM t1
INNER JOIN t2 ON t1.t1_name = t2.t2_name;
You might also need to have a look at the different types of JOIN, see this for more information:
A Visual Explanation of SQL Joins.
Try this query
select t1.t1_value
from t1
inner join t2 on t1.t1_name=t2_name
For more study you can refer INNER JOIN
And Different type of JOIN
A VISUAL REPRESENTATION OF JOIN
Select t1_value from T1, T2 where T1.t1_name = T2.t2_name
SELECT t1_name.t1, t2_name.t2 FROM t1, t2 WHERE t1.t1_name = t2.t2_name
That should work as expected, just to clarify you can also use INNER JOIN syntax as referenced here : http://www.tizag.com/mysqlTutorial/mysqlleftjoin.php
You can achieve by just joining two tables t1 and t2.
SELECT
t1.Name,
t1.value
FROM t1,t2
WHERE t1.t1_name = t2.t2_name
Related
I work on a query in mysql that spend 30 sec to execute. The format is like this :
SELECT id
FROM table1 t1
INNER JOIN table2 t2
ON t1.id = t2.idt2
The INNER JOIN take 25 of 30 sec. When I write this like this :
SELECT id
FROM table1 t1
INNER JOIN (
SELECT idt2,col1,col2,col3
FROM table2
) t2
ON t1.id = t2.idt2
It take only 8 sec! Why does it work? I'm afraid of losing data.
(obviously, my query is more complex than this one, it's just an exemple)
Well you haven't shown us the EXPLAIN output
EXPLAIN SELECT id
FROM table1 t1
INNER JOIN table2 t2
ON t1.id = t2.idt2
this would definitly give us some insights of your query and table sctructures.
Based on your scenario, 1st query seems like you have issues with indexing.
What happened in your 2nd query is the optimizer is creating a temporary set from your subquery furthering filtering your data. I dont recommend doing that in MOST cases.
Purpose of subquery is to solve complex logic, not an instant solution for everything.
I want to display the ID from Table1 (TID) and the results of an inner join.
The following statement is not working.
Situation: Two Tables:
Table 1 PK:TID, FK: Table2_PID
Table2 PK: PID, Name
Among other data I want to display the Name of every PID in Table1 which is stored in Table2.
SELECT T.TID
,(Select P.Name
from mydb.Table2 P
inner join mydb.Table1 T
on P.PID=T.Table2_PID)
FROM mydb.Ticket T;
Result: Error Code 1242. Subquery returns more than 1 row
I do know that the result returns more than 1 row, but I want to show the Name of every PID in Table1 which is stored in Table2. So any ideas on how I can do that?
PS: I'm using mySQL and working with MySQL Workbench v6.3
You don't need use the inner joins for getting all the names of the ID. You can try the default join to achieve the result.
select t2.pid, t2.name from mydb.Table2 t2, mydb.Table1 t1 where t1.pid = t2.pid;
Hope this helps.
You must use join like this
select t1.TID,t2.Name from Table1 t1 left join Table2 t2 on t1.Table2_PID=t2.PID
Thanks for the response, but the question/problem still remains.
It wasn't about the join.
It is about the subquery and selecting multiple rows within it.
Thank you guys,
I was thinking of a solution way to complicated. I resolved it using a simple where statement.
SELECT T.TID, P.Name
FROM mydb.table1 T, mydb.table2 P
WHERE P.PID=T.table2_PID;
I have to tables who looks like this
Table1
User_ID(int)|comment(text)|gender(int)
so it could be like 1|bla bla|1
Table2
ID(int)|Username(text)
Now I want to build a query like
SELECT Table1.User_ID,Table1.comment,Table1.gender FROM Table1 INNER JOIN Table2 ON Table1.User_ID=Table2.ID SELECT Username
Is something like that working? I hope my query is not that bad to understand. If thats working in one way or another, is it also possible to make some more joins?
First time I have to work with joins. I'm a bit irritated, most examples are a bit abstract,..
Just list every columns that you want to select after the SELECT statement, even if they come from joined tables.
SELECT Table1.User_ID, Table1.comment, Table1.gender, Table2.Username
FROM Table1 INNER JOIN Table2
ON Table1.User_ID = Table2.ID
You could have as many joins as you want:
SELECT Table1.User_ID, Table1.comment, Table1.gender, Table2.Username
FROM Table1
INNER JOIN Table2
ON Table1.User_ID = Table2.ID
INNER JOIN Gender
ON Table1.gender = Gender.id
Yes and yes. You can access table 2 fields in select statement also.
I want to do a sql query and have some problems:
I want to salect from table_1 the ID's Where parent_id is the value I have:
SELECT ID FROM table_1 WHERE parent_ID = 'x'
I want to use the ID'S I got in 1. and
SELECT FROM table_2 WHERE ID = 'The ID's from Query 1.
Simple and Execute
Select t1.`id` FROM table t1 INNER JOIN table t2 ON t1.`id`=t2.`id`
As Bainternet mentioned you can do this with a subquery
SELECT * FROM table_2 WHERE ID IN (SELECT ID FROM table_1 WHERE parent_ID = 'x')
Though your idea to use an inner join is also good (especially since MySQL can be slow when processing subqueries).
SELECT t2.* FROM table_2 as t2 INNER JOIN table_1 AS t1 ON t2.ID = t1.ID WHERE t1.parent_ID = 'x'
If that's not clear, try looking at the MySQL JOIN Syntax or the Subqueries, as Bainternet mentioned. If these examples and the MySQL docs aren't clear enough for you, consider posting more details on exactly what you're trying to do (e.g. include table structures in your question). Also while you might want this information for some WordPress related work you are doing, there's nothing in the question itself that actually ties it to WordPress. So if you have more questions that about MySQL queries in general, then you might want to consider posting them to the StackOverflow, tagged as mysql-query.
I have stumped on this as I am a total beginner in MySql.
Here is a the basic of how the two tables are formed
Table 1
id,product_id, product_name
Table 2
id,product_id,active
Now i know how to do a select statement to query the results from one table but when I have to involve two, I am lost. Not sure if I have to use inner join, left join etc.
So how can I return the results of the product_id from table 1 only if in table 2 is active?
You could use JOIN (as Fosco pointed out), but you can do the same thing in the WHERE clause. I've noticed that it's a bit more intuitive method than JOIN especially for someone who's learning SQL. This query joins the two tables according to product_id and returns those products that are active. I'm assuming "active" is boolean type.
SELECT t1.*
FROM Table1 t1, Table2 t2
WHERE t1.product_id = t2.product_id AND t2.active = TRUE
W3Schools has a good basic level tutorial of different kinds of JOINs. See INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN.
It's pretty simple to join two tables:
select t1.*
from Table1 t1
join Table2 t2 on t1.product_id = t2.product_id
where t2.active = 'Y'