Why query show only first row? - mysql

We have 2 tables - Table1 and Table2
rows in Table1
id name
1 test
2 test2
Table2 is empty, structure:
id table1_id
query:
SELECT
t1.name as name,
count(t2.id) as count_show
FROM
Table1 as t1
LEFT JOIN
Table2 as t2 on t2.table1_id= t1.id
In result we see:
name count_show
test 0
We know that problem with count, but why? where error?
Why we get only first row and how output all rows?

Answer as requested:
You're doing a count. It is automatically grouped. So if the count of your row is only one row, that's what you get.

You have to add Group by t1.id To the end of your query.
The final query:
SELECT t1.name AS name, COUNT(t2.id) AS count_show
FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON t2.table1_id = t1.id
GROUP BY t1.id

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.

Conditional SELECT SQL with relational tables

Say I have an id in table1 which is a foreign key in table2 and there is a column in table2 called condition.
I need to select all ids from table 1 that aren't in table2 where condition = 1.
So for id in table 1 "select it" if it is not in table2 where condition = 1.
Edit: I used Ahsan Habib's answer and it worked great!
if you just want to select ID column from table1 this will work fine....Its just a simple set operation
select id from t1
minus
select id from t2 where condition = 1;
for all column you may try
select * from t1 whare id not in (select id from t2 where condition = 1);
This is almost a direct translation of what you are asking for:
select t1.*
from table1 t1
where t1.id not in (select t2.id from table2 t2 where t2.condition = 1);
Another way using NOT EXISTS
select t1.*
from table1 t1
where NOT EXISTS(select 1 from table2 t2 where t1.id = t2.id and t2.condition = 1);

Validate that all rows from INNER JOIN have value

I have a one-to-many table relation between table1.id and table2.parent_id and want to select table1.id based on table2.status.
SELECT table1.id FROM table1
INNER JOIN table2 ON table2.parent_id = table1.id
WHERE table2.status = 1
This does pretty much what I want if there is only one relation in table2. But if there are more rows in table2 one result may have status=1 but the other have status=2.
What I want is to get a result for table1.id only if all results in table2 is of status=1.
Example;
table1
id=1, name=row1
id=2, name=row2
table2
id=1, parent_id=1, status=1
id=2, parent_id=1, status=2
id=3, parent_id=2, status=1
id=4, parent_id=2, status=1
In the example above table1.id = 1 has 2 relations with different statuses, I don't want this row. table1.id = 2 however have 2 relations with the same status=1, I want this result.
You can use EXISTS and NOT EXISTS to check for values in the other table.
This query selects all rows from Table1 that do have matching records in Table2, but none of those have a status different than 1.
SELECT *
FROM Table1 t1
WHERE -- Check for matching records in Table2.
EXISTS
(SELECT 'x' FROM Table2 t2
WHERE t2.parent_id = t1.id)
-- Skip rows that have a status different than 1.
AND NOT EXISTS
(SELECT 'x' FROM Table2 t2
WHERE t2.parent_id = t1.id
AND t2.status <> 1)
I wasn't sure what you want to have if there is not status in Table2 at all. If you want to return rows from Table1 that don't have any matching rows in Table2, you can just omit the first EXIST <subselect> part.

sql join three table one maybe empty

I have three tables, two certainly with data values, one the values can be present or not.
This is an example schema
Table 1
id, username
Table 2
id, street
Table 3
id, phone_number (this can be not present)
please help me with query
SELECT t1.ID, t1.Username, t2.street, t3.phone_number
FROM Table1 as t1
INNER JOIN Table2 as t2 on t1.id = t2.id
LEFT OUTER JOIN Table3 as t3 on t1.id = t3.id

how to display two table records (selected column)

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.