how to select from two not joined tables in mysql - mysql

I have two tables that have no common column. But there is relation. I have table1, table2. table 1 is as follows:
t1.ID | t1.Name | t1.Number
Where Name is unique value.
table2 is as follows:
t2.ID | t2.Number1 | t2.Number2 | t2.Country
My query is as follows:
select t1.Name, t1.Number, t2.country
from db.t1, db.t2
where t1.Number between t2.Number1 AND t2.Number2
What is happening as a result from the query is that I get each record twice. But, when I add:
group by t1.Name
I get the correct result (each record once). I do not want to use group by. How to make correct query and do I get the same record twice without group by ?

Try using DISTINCT :
SELECT DISTINCT 1.Name, t1.Number, t2.country
FROM db.t1, db.t2
WHERE t1.Number BETWEEN t2.Number1 AND t2.Number2

There should be at least one column that is common to both of the tables. Else you 'll get duplicate values only.
At least to my knowledge that is the case.

select t1.Name ,t1.Number from t1
union
select t2.Number1 ,t2.Number2 from t2
You can use union in this case.

Related

MySQL : Get count of rows containing name from different table, both tables are in relation

I have 2 tables in my database, let's say t1 and t2.
There are 2 rows in both of them.
1s is id and 2nd is Name
Table t1:
Id. Name
1. abc
2. def
3. ghi
Table t2:
Id. Name
1. abc
2. abc
3. abc
4. def
4. def
Now , I need this type of output in MySQL
Total. Name
3. abc
2. def
0. ghi
I tried this so far
Select Count(*) as Total, Name
from t1 Inner Join
t2
Group By t2.Name
Having t1.Name = t2.Name
SELECT COUNT(t2.Id) as Total, t1.Name
FROM t1 LEFT JOIN t2
ON t2.Name = t1.Name
GROUP BY t1.Name;
The same answer like Yogesh, but with the comma and caps correctly set.
regarding the SELECT you made
Select Count(*) as Total, Name
from t1 Inner Join
t2
Group By t2.Name
Having t1.Name = t2.Name
here my 2 cents
1st.) as there are two tables with the same column you will receive ambiguous column error, so you have to write "t1.Name" instead of just "Name"
2nd.) with an INNER JOIN you select always and only the rows that exist in both tables linked with fields at the ON part of the join
3rd.) and the HAVING clause is not a appropriate way to link tables together, use just the complete JOIN syntax with the ON and the according fields
Hope that helps a bit.
Simply you can do LEFT JOIN & aggregation :
SELECT COUNT(t2.id), as total t1.name
FROM t1 LEFT JOIN
t2
ON t2.name = t1.name
GROUP BY t1.name;

Is there a more efficient way using mysql to get a list for a given column in an sql result?

I have a query that rows of a table each containing an id.
For each id I want to get multiple values from another table.
The way I would do this is make the first query, then loop through the result making a query for each id.
This could mean making a thousand queries, is there a way I could do this in 1 query.
I think you want group_concat(). Something like this:
select t1.id, group_concat(t2.othercol)
from table1 t1 join
table2 t2
on t1.id = t2.id
group by t1.id;
Or perhaps you just want in:
select t2.*
from table2 t2
where t2.id in (select t1.id from table1 t1);

SQL SELECT from multiple tables or JOIN

I have two tables from which I need to get data in the same SELECT output. The thing is that I need to limit the amount of results.
Say I have an ID column that is unique in table1, but in table2 it has many rows with that ID.
Now I just want to list how many different IDs I have in table1 and some other information stored in table2.
How can I get the desired output I show in the end?
To make my idea clear I used a "messenger" database for an example.
Tables
T1
Id_thread Date
1 13Dic
2 12Dic
T2
Id_thread Message Name
1 Hi Someone
1 Hi to you Someone
2 Help me? Someother
2 Yes! Someother
Desired output
T1.Id_thread T2.Name T1.Date
1 Someone 13Dic
2 Someother 12Dic
I'd join and use distinct:
SELECT DISTINCT t1.id_thread, t2.name, t1.date
FROM t1
JOIN t2 ON t1.id_thred = t2.id_thread
Use a JOIN and GROUP BY:
SELECT t1.Id_thread, t2.Name, t1.Date
FROM t1
JOIN t2 ON t1.Id_thread = t2.Id_thread
GROUP BY t1.Id_thread
Note that if Name is the same for all rows in t2 that have the same Id_thread, that column probably should be in t1. If you fix that, you don't need the JOIN.
Try this:
SELECT DISTINCT T1.Id_thread, T2.Name, T1.Date
FROM T1
LEFT OUTER JOIN T2 ON T1.Id_thread = T2.Id_thread
select T1.Id_thread,T2.Name,T1.Date from T1
inner join T2 on T1.Id_thread = T2.Id_thread
group by T1.Id_thread
order by T1.Id_thread
You haven't specified how you want the limit the results from Table 2. Considering you just want one row, you can use a CROSS APPLY:
Select T1.Id_thread,T2Table.Name,T1.Date From T1
Cross Apply (Select Top 1 T2.Name From T2 Where T2.Id_thread=T1.Id_thread) T2Table
You can specify other conditions in the inner Select statement if you wish.

Select both ID columns when using UNION and GROUP BY

I'm desperate with this query. I have two tables table1 and table2, tables are identical but they have different data. I'm trying to remove duplicities by columns code and manufacturer. To do that I need in final result ID from table1 ID from table2 and also columns code and manufacturer
SELECT * FROM (
SELECT id,code,manufacturer FROM table1 WHERE manufacturer = 1
UNION SELECT id,code,manufacturer FROM table2 WHERE manufacturer = 1
) AS t GROUP BY code HAVING COUNT(*) > 1
But in result i got only values from table1. It's OK but I just need to get there id from table2 too. Please can anyone give me some tips how to do this ?
You have two basic problems:
Problem 1:
You are using UNION when you should be using UNION ALL, because UNION removes duplicates!
Problem 2:
This isn't the right way to go about the problem. You should be using a simple join, not a union.
Try this:
SELECT
t1.id as table1_id,
t2.id as table2_id,
t1.code,
t1.manufacturer
FROM table1 t1
JOIN table2 t2
ON t2.code = t1.code
AND t2.manufacturer = t1.manufacturer
WHERE manufacturer = 1 -- this WHERE clause is optional
Your use of the WHERE clause is a little odd - consider removing it to get all duplicates from all manufacturers.

How to avoid "Ambiguous field in query" without adding Table Name or Table Alias in where clause

I have a select query in which I have joined a couple of tables say T1 and T2 and both the tables have a field named STATUS which I don't need to fetch. In the where clause I need to add WHERE STATUS=1 and some more conditions.
But somehow I just can't add the table name or table alias to the field in the where clause i.e. I can't use where T2.STATUS=1. Is there any way to always consider the STATUS=1 from the where clause being T1.STATUS so that I can avoid "Ambiguous field error"?
Here is a sample query:
select T1.name, T1.address, T1.phone, T2.title, T2.description from T1
Left Join T2 on T1.CID=T2.ID
where STATUS = 1
In above query, I want the STATUS =1 to always mean T2.STATUS
If you for some reason can't live with doing
select T1.name, T1.address, T1.phone, T2.title, T2.description from T1
Left Join T2 on T1.CID=T2.ID
where T2.STATUS = 1
Then I guess you could
SELECT T1.name, T1.address, T1.phone, T2.title, T2.description
FROM ( SELECT CID, name, address, phone
FROM T1) AS T1
LEFT JOIN T2
ON T1.CID=T2.ID
WHERE STATUS = 1
Basicly just skip getting the STATUS column from T1. Then there can be no conflict.
Bottomline; there's no simple way of doing this. The one closest to simple would be to have different names of both STATUS columns, but even that seems extreme.