how to display two table records (selected column) - mysql

I created two tables:
Name:table1,table2
table1 consists of:id,name,contactnumber
101,john,9955443322
102,peter,9955443311
table2 consists of:id,place,date
101,chennai,15-05-2014
102,munbai,13-05-2014
select table1.id
,table1.contactnumber
,table2.date
from table1,table2
where table2.date = 29-09-2014
&& table2.loannumbers=table1.loannumber
But returned empty result set.
I want to display columns:
id,name,date
I want to display rows :
(table2)date=15-05-2014 and (table1)id=id(table2).

Try this
SELECT table1.id, table1.name, table2.date
FROM table1
INNER JOIN table2 ON table1.id = table2.id;

First of all, don't use this syntax for jointures between tables. This is an old school notation and using explicit jointures will be much more readable.
Here is the query you're looking for:
SELECT T1.id
,T1.name
,T2.date
FROM table1 T1
INNER JOIN table2 T2 ON T2.id = T1.id
AND T2.date = '2014-05-15'
Hope this will help.

Related

Join two tables based on a view, sql

I have the following sql situation
Table1
id name
Table2
id name
_Table1ToTable2
id, id_table1, id_table2
Result:
id, name, table2_name, table2_id
I would like to join Table1 and Table2 into a single query, but I cannot find a way to make a join between them, any ideas?
It seems like you want to use bridge table Table1ToTable2 to join Table1 and Table2. This can be done simply with 2 INNER JOINs :
SELECT
tt.id AS tt_id,
t1.id AS t1_id,
t1.name AS t1_name,
t2.id AS t2_id,
t2.name AS t2_name
FROM
Table1ToTable2 AS tt
INNER JOIN Table1 AS t1
ON t1.id = tt.id_table1
INNER JOIN Table2 AS t2
ON t2.id = tt.id_table2
if there is no relationship between table1 and table 2 then,
select table1.id, table1.name, table2.id, table2.name from
table1, table2
if table1 and table2 are related with ID then,
select table1.id, table1.name, table2.id, table2.name from
table1
inner join table2
on table1.id = table2.id
If your intention is to retrieve a result set with every possible combination of the id columns from both tables then you can do as follows:
select id = identity (int, 1, 1), * into #a from (select distinct table1. id as id1, table2. id as id2 from table1 cross apply table2) d; select * from #a; drop table #a
If you do not wish to use a temp table an alternative would be to use some variation of the row_number() function.
The above solves for an assumed requirement as your question lacks context. Elaborate on your requirements and we will be able to provide you with a more relevant and detailed answer.

Pull Data from 3 mySQL Tables

I have a query that selects all the data from one table and specific columns from another where two columns are equal and another column equals a specific value.
SELECT table1.*, table2.column1, table2.column2 FROM table1
INNER JOIN table2 ON table1.column3=table2.column3
WHERE table1.column1='foo';
Is it possible to pull table3.column1 & table3.column2 from a table3 where table3.column3=table1.column3 AND table3.column4='bar' ?
The thing that makes it more complex is that the data in table3 is optional and may not always exist; however I still want the query to return table1 & table2's data but with table3.column1 & table3.column2 just being presented as NULL or EMPTY...
Im struggling to get my head around it myself, and any insight or assistance would be greatly appreciated.
Use left join:
SELECT table1.*, table2.column1, table2.column2, table3.column1, table3.column2
FROM table1
INNER JOIN table2
ON table1.column3 = table2.column3
LEFT JOIN table3
ON table3.column3 = table1.column3 AND table3.column4 = 'bar'
WHERE table1.column1 = 'foo';
When there is no corresponding record in table3 table3.column1 and table3.column2 will be null
And if column1 and column2 names are not unique column names in select clause you need to give them an alias
select t1.col1, t2.col2, t3.col3
from table1 as t1
inner join table2 as t2 on t2.col3.t1.col2
left join table3 as t3 on t3.col3.t2.col2
where t1.col1 = 'test';
it can be done like this.

Return multiple rows from subquery

Basically I have two tables. 1 and 2. I need the field2 column in the table2 table to return multiple rows. I tried the below join (simplified the columns) but unfortunately it returns me only one result.
SELECT table1.field1, table1.field2, table1.field3, sub_q.field4
FROM table1
JOIN (
SELECT t2.field4, t2.filter1, t2.filter2 FROM table2 t2
) sub_q ON (sub_q.filter1 = table1.id AND sub_q.filter2 = 1)
##Should return multiple rows
##but returns only one!
WHERE table1.id = ..;
Edit:
I created a schema here: http://sqlfiddle.com/#!9/1c5737 with the select query as
SELECT t1.field1, t1.field2, t1.field3, t2.field1
FROM table1 t1
JOIN table2 t2 ON t2.filter1 = t1.id AND t2.filter2 = 1
WHERE t1.id = 1;
Only to find out that it works there, so I come back in shame to accept the answer and check where I messed up in my query (probably one of the fields)
Why are you using a subquery in the join? This is how it should be written:
SELECT table1.field1, table1.field2, table1.field3, t2.field1
FROM table1 t1
JOIN table2 t2 ON t2.filter1 = table1.id AND t2.filter2 = 1
Also it is likely that you need LEFT JOIN (or INNER JOIN) instead of JOIN, but cannot be sure without more details on what you're trying to achieve.

mySQL JOIN with specified keyword

What is the best way to join 2 tables where the second table has an id and a keyword to join?
my try :
SELECT id, name
FROM table1
LEFT JOIN table2 ON (table1.id = table2.id AND table2.id = 'myKeyword')
WHERE ...
Is there a way to handle the search / join of the keyword in the WHERE clause?
Placing table2.id = 'myKeyword' in the where clause will negate the LEFT JOIN
This is quite appropriate as you have it.
Maybe with a little more detail we can see what you are getting at.
SELECT {column_list}
FROM table1 t1, table2 t2
WHERE t1.id = t2.id
AND {some_column_from_either_table} = 'myKeyword';

MYSQL select from 3 or more tables

I need to make a MySQL query to show data from 3 diferents tables.
This is the table 1:
TABLE1
id
reference
name
email
This is the table 2:
TABLE2:
id
phone
This is the table 3:
TABLE3:
id
phone
I need to show all data from table1, and also the phone from table2 or table3, only if the id in table2 or table3 is the same number that is in the reference field in table1.
Any advice? Thank you!
You can try something like
SELECT t1.*
COALESCE(t2.phone,t3.phone) phone
FROM Table1 t1 LEFT JOIN
Table2 t2 ON t1.reference = t2.id LEFT JOIN
Table3 t3 ON t1.reference = t3.id
Have a look at COALESCE(value,...) and maybe SQL SERVER – Introduction to JOINs – Basic of JOINs
Yes, I have an advice, modify your structure. There's no point in having different tables to hold different phone numbers.
Here's something you can do:
table1( -- you should give it a better name
id,
-- reference, -- not needed now...
name,
email
);
phone_numbers(
id,
table1_id,
phone
);
Now you can do something like:
SELECT table1.*, GROUP_CONCAT(phone)
FROM table1
LEFT JOIN phone_numbers ON table1.id = table1_id
GROUP BY table1.id, name, email -- , whatever fields you have more on table1
You asked for a phone from table2 or from table3.
Because these 2 tables have common columns, we can simplify this whole thing and think about these 2 tables as a single one, by using an UNION clause:
select table1.*, v.phone
from table1
inner join (select * from table2
union
select * from table3) v on v.id = table1.reference
EDIT: corrected table names in the union
SELECT t1.*, t2.*, t3.*
FROM table1 t1 JOIN table2 t2
ON t1.reference = t2.ID
JOIN table3 t3
ON t1.reference = t3.ID
I don't know if you can do CASE statement in select in mysql, but you can try a CASE statement as a column and join. Here is some sudo code.
SELECT t1.*, CASE t2.phone IS NOT t3.phone THEN t3.phone ELSE t2.phone END CASE as PhoneNumber
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.reference = t2.id
LEFT JOIN Table3 t3 ON t1.reference = t3.id