How to get data from 2 mysql tables - mysql

what is the syntax if I want to utilize 2 or more tables for my mysql query.
Example, I'm going to fetch the idnumber from the 1st table and the religion on the 2nd table. And the query will return the combined version of those 2 tables showing only the religion and idnumber.
The code might look something like this , but it doesn't work:
select t1.IDNO, t1.LNAME t2.RELIGION from t1, t2 where t2.IDNO='03A57'

The SQL query would be as follows:
SELECT a.idnumber, b.religion FROM table1 a, table2 b
You can add conditions from both tables as well by doing the following:
SELECT a.idnumber, b.religion FROM table1 a, table2 b WHERE b.religion = 'Christian'
More information can be found in this thread: http://www.astahost.com/info.php/mysql-multiple-tables_t12815.html

SELECT t1.IDNO, t1.LNAME FROM t1 LEFT JOIN t2.RELIGION ON ( t2.IDNO = t1.IDNO )
(more or less)
The Join is the command that will link the two.
http://dev.mysql.com/doc/refman/5.0/en/join.html

The code below would do a cross-join.
SELECT tb1.id, tb2.religion FROM tb1 JOIN tb2 ON (tb1.religion_id = tb2.religion_id) WHERE t2.IDNO='03A57'
Again... see http://dev.mysql.com/doc/refman/5.0/en/join.html...

Related

LEFT JOIN to show NULL rows + WHERE

I have two tables, one which contains unique names (bill_datatypes), and another one which expresses that these names are connected to some subcategories (one name can be connected to more categories as well).
The first table (relevant part):
The second table (relevant part):
I would like to select all the names which are not connected to a specific subcategory yet. This means that the names might be mentioned in the connecting table but connected to a different subcategory.
I have solved the problem with the following subquery:
SELECT T1.name
FROM bill_datatype T1
WHERE NOT EXISTS (
SELECT T2.bill_datatype_id
FROM obligatory_field T2
WHERE T2.bill_datatype_id = T1.id AND T2.bill_sub_category_id = 1
)
This solution seems to work well, but this way I can not have any data from the second table, which might be needed in the future.
Do you have any suggestions on how to solve the problem with a LEFT JOIN? (The WHERE clause on the subcategory made it too challenging for me.)
Thank you for your help also in advance!
From how I understand your question, I think this query will achieve what you want:
SELECT T1.name
FROM bill_datatype T1
LEFT JOIN obligatory_field T2
ON T2.bill_datatype_id = T1.id AND T2.bill_sub_category_id = 1
WHERE T2.bill_datatype_id IS NULL
The WHERE condition on the JOIN'ed table will give you only the names from T1 which have no bill_sub_category_id or their bill_sub_category_id is not 1.
SQLFiddle
SELECT distinct T1.name
FROM
bill_datatype T1
right join
(
SELECT T2.bill_datatype_id
FROM obligatory_field T2
WHERE T2.bill_sub_category_id = 1
)
as T3
on T1.id != T3.bill_datatype_id
http://sqlfiddle.com/#!9/d4f616/18

Using subquery in MySQL

I need some help with a specific problem about subqueries in MySQL. This is a shortversion of the problem I have:
I have table 1 and 2 where table 1 have a column called "SupplyID" which corresponds with table 2's "ID". I need to get a list with stuff from one single supplier.
I tried with this but didn't work:
select name
from item
where SupplyID exists (
select *
from SupplyID
where SupplyID = ID
);
Assuming your tables are named table1 and table2 you
You could use a inner join
select distinct t1.name
from ybale as t1
inner join table2 as t2 on t1.ID = t2.SupplyID
Try this:
select name from item i where exists (select * from table2 t where i.SupplyID = t.ID);
My answer is more like to what scaisEdge answered here but I strongly recommend to do this with LEFT JOIN. and If you want to select just one ID, for example ID=10
SELECT T1.name
FROM item AS T1
LEFT JOIN SupplyID AS T2 ON T2.SupplyID=T1.ID
WHERE T1.ID = 10

SQL select data from multiple tables with subqueries (including data from inner join) Error: 1242

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;

Joins with a SELECT command

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

SQL: How can we make a table1 JOIN table2 ON a table given in a field in table1?

Imagine I have table1 which has a column named 'table_name'. I use table1.table_name to store the name of another table in the database. The referenceable tables would all have a field 'target_id.
Is is possible to use table_name in a JOIN statement?
For example:
SELECT t1.*, t2.* FROM table1 AS t1
JOIN table1.table_name AS t2 ON t1.table1_id = t2.target_id
The obvious solution is to use the script (C++ in my case) to get the table name first, and construct a SQL query from it. The question is: can we bypass the script and do this directly in SQL (MySQL)?
Edit: What is dynamic SQL?
The only chance you have is to do 2 SQL statements:
select the tablename you need
use this table-name to dynamically build the secound query to get the data you need - what you want isn't possible to do with SQL directly (and it sounds like you've designed your database wrong in some way - but that's hard to say without knowing what's the goal of it).
I know I'm late to the party, but I wanted to offer a different solution. I see this sort of thing a lot in audit tables. The column table_name would refer to "what table was changed" and table1_id would refer to the ID of the row that changed in that table. In this case, the audit table is pointing back to many different tables that don't normally get joined.
Here goes:
SELECT t1.*, t2.*, t3.*, t4.*, t5.*
FROM table1 AS t1
left JOIN table2 AS t2
ON t1.table1_id = t2.target_id
and t1.table_name = 'table2'
left JOIN table3 AS t3
ON t1.table1_id = t3.target_id
and t1.table_name = 'table3'
left JOIN table4 AS t4
ON t1.table1_id = t4.target_id
and t1.table_name = 'table4'
left JOIN table5 AS t5
ON t1.table1_id = t5.target_id
and t1.table_name = 'table5'
Of course, the main drawback is that each table that can be possibly referenced needs to be explicitly included in the SQL command.
You can get more elegant output using this as your select list:
SELECT
t1.*,
coalesce(t2.fieldA, t3.fieldA, t4.fieldA, t5.fieldA) as fieldA,
coalesce(t2.fieldB, t3.fieldB, t4.fieldB, t5.fieldB) as fieldB
etc