COUNT function with INNER JOIN - mysql

I have the following table:
A FromA User
1 NULL Bob
2 1 Bob
3 1 Chris
4 2 Chris
User is the name of the person who created item A. FromA is the source that the User grabbed it from.
I want to figure out is Chris' most frequency source.
My query
SELECT count(T1.A GROUP BY T1.User), T1.User
FROM Table T0
INNER JOIN Table T1 ON T0.FromA=T1.A
WHERE T0.User='Chris'
It should return Bob=2. But it doesn't seem to work.

Try this:
select t2.user, count(*) Total from t t1
join t t2 on t1.fromA = t2.a
where t1.user = 'Chris'
group by t2.user
order by Total desc
limit 1
The limit 1 will give you just the most frequently used source.
Edit:
inner join will fail to fetch record=>1 | NULL | Bob as 'fromA' column has null value. so, switch to left join – Angelin Nadar
Nope. Here is a working example

You just have your group by in the wrong place. This should work
SELECT count(T1.A ), T1.User
FROM Table T0
INNER JOIN Table T1 ON T0.FromA=T1.A
WHERE T0.User='Chris'
GROUP BY T1.User

Try with this, I have changed table aliases:
SELECT count(*), t_User.User
FROM
Table t_FromA
LEFT OUTER JOIN
Table t_User
ON t_User.FromA=t_FromA.A
WHERE t_FromA.User='Chris'

Related

Selecting Counts from Different Tables with a Subquery

I'm new to MySQL, and I'd like some help in setting up a MySQL query to pull some data from a few tables (~100,000 rows) in a particular output format.
This problem involves three SQL tables:
allusers : This one contains user information. The columns of interest are userid and vip
table1 and table2 contain data, but they also have a userid column, which matches the userid column in allusers.
What I'd like to do:
I'd like to create a query which searches through allusers, finds the userid of those that are VIP, and then count the number of records in each of table1 and table2 grouped by the userid. So, my desired output is:
userid | Count in Table1 | Count in Table2
1 | 5 | 21
5 | 16 | 31
8 | 21 | 12
What I've done so far:
I've created this statement:
SELECT userid, count(1)
FROM table1
WHERE userid IN (SELECT userid FROM allusers WHERE vip IS NOT NULL)
GROUP BY userid
This gets me close to what I want. But now, I want to add another column with the respective counts from table2
I also tried using joins like this:
select A.userid, count(T1.userid), count(T2.userid) from allusers A
left join table1 T1 on T1.userid = A.userid
left join table2 T2 on T2.userid = A.userid
where A.vip is not null
group by A.userid
However, this query took a very long time and I had to kill the query. I'm assuming this is because using Joins for such large tables is very inefficient.
Similar Questions
This one is looking for a similar result as I am, but doesn't need nearly as much filtering with subqueries
This one sums up the counts across tables, while I need the counts separated into columns
Could someone help me set up the query to generate the data I need?
Thanks!
You need to pre-aggregate first, then join, otherwise the results will not be what you expect if a user has several rows in both table1 and table2. Besides, pre-aggregation is usually more efficient than outer aggregation in a situation such as yours.
Consider:
select a.userid, t1.cnt cnt1, t2.cnt cnt2
from allusers a
left join (select userid, count(*) cnt from table1 group by userid) t1
on t1.userid = a.userid
left join (select userid, count(*) cnt from table2 group by userid) t2
on t2.userid = a.userid
where a.vip is not null
This is a case where I would recommend correlated subqueries:
select a.userid,
(select count(*) from table1 t1 where t1.userid = a.userid) as cnt1,
(select count(*) from table2 t2 where t2.userid = a.userid) as cnt2
from allusers a
where a.vip is not null;
The reason that I recommend this approach is because you are filtering the alllusers table. That means that the pre-aggregation approach may be doing additional, unnecessary work.

Have a left join where duplicates in the second table is involved - MYSQL

Table 1:
user score
------------
A 1
B 2
Table 2:
user comment time
----------------------------
A good <timestamp 1>
A bad <timestamp 2>
B average <timestamp 3>
I want to join these two tables such that I get the below:
user score comment
-------------------------
A 1 good
B 2 average
As you can see I'll need to join the second table's comment based on the timestamp (the most recent timestamp). I tried
SELECT st.user as user,st.score,
case when v.comment is null then 'NA' else v.comment end as comment
FROM tale1
left JOIN (select distinct user,comment,max(time) from table2) v ON st.user=v.user
but this doesnt work.
You can join with a correlated subquery that filters on the latest timestamp:
select
t1.*,
t2.comment
from table1 t1
left join table2 t2
on t2.user = t1.user
and t2.time = (
select max(t22.time)
from table2 t22
where t21.user = t1.user
)
Side note: I am unsure that you do need a left join here (your sample data does not demonstrate that).
You only want one column from table2 so I recommend a correlated subquery:
select t1.*,
(select t2.comment
from table2 t2
where t2.user = t1.user
order by t2.time desc
limit 1
) as comment
from table1 t1;
This query will make optimal use of an index on table2(user, time desc, comment) -- alas, though, I think the desc is ignored in MySQL.

SELECT from TWO tables without some records

I have TWO tables:
t1 contain ID_car (unique), name
t2 contain ID_car(from t1), status (many status records on same ID_car)
And i need the following result:
All ID_car FROM t1 WITHOUT status = something
I already try it INNER, LEFT, RIGHT JOIN and didn't work.
How can i do that?
Many thanks for help!
More details:
t1
------------
ID_car name
------------------
1 Toyota
2 Honda
3 Mazda
4 Ford
t2
-----------------
ID_car status
1 ok
1 not_ok
2 ok
4 not_ok
ID_car 3 din not have any records in t2 but i want to display result
And i need the following result (all car from t1 without car status not_ok):
the expected result
-----------------
ID_car status
2 ok
3
Update 2
Finally solved! Thanks for help!
That's works for me:
SELECT * FROM t1
WHERE t1.ID_auto NOT IN
(SELECT DISTINCT t1.ID_auto FROM t1, t2 WHERE t1.ID_auto = t2.ID_auto AND t2.category='not_ok')
-- updated per comment below. This update will return all records from T2 (even if not existing in T1) but only if T2.Status != something
-- this should do what you want. It will give you all the records in T1, and any data in T2 (but not required to be in that table) where your T1.status is not something
SELECT *
FROM t1
LEFT JOIN t2 ON T1.ID = T2.ID
-- did the logic on the JOIN here instead of in where clause, because doing in where clause would force records to appear in t2 table (basically converting it to an inner join) doing it in the join itself does not cause this to happen
AND T2.Status != 'something'
SELECT ID_car
FROM t1
LEFT JOIN t2
ON t1.ID_car=t2.ID_car
WHERE t2.status=something
SELECT ID_car
FROM t1
LEFT JOIN
t2 on t1.ID_car=t2.ID_car
WHERE NOT t2.status='something'
Created from top of my head, hopefully it works!
Maybe if you could post the queries you wrote, maybe they could show us why the joins don't work because it should.
I do not know if I understand correctly but try and let me know:
SELECT DISTINCT t1.ID_car FROM t2 INNER JOIN t1 ON t2.ID_car = t1.ID_car WHERE t2.status != 'something'

how to make query show the an other column of the primary key?

umm I'm not sure I've made the title right but its kind of hard to express it in short words.
I have to tables
table1:
id | name
1 | alice
2 | bob
table 2:
user_id | date
2 | 2014-11-1
2 | 2014-11-2
1 | 2014-11-3
as a query, if I want to show table 2 but instead of the integer numbers of user_id, I want it to show the corresponding names of the users where this info is stored in table 1.
I think this is supposed to be easy but I don't know how to get this done.
A query along the lines of -
select t1.name, t2.date
from table_1 t1 inner join table_2 t2 on t1.id = t2.user_id
Try:
SELECT t2.user_id, t1.name
FROM table1 t1 INNER JOIN table2 t2
ON t1.id = t2.user_id
This will do it.
SELECT
`b`.`name`,
`a`.`date`
FROM
table2 a
INNER JOIN table1 b ON (a.user_id = b.id)
SELECT
B.[Name]
,A.[date]
FROM [table 2] A
LEFT OUTER JOIN [table1] B
ON A.[user_id] = B.[id]

How to get value from two table if id is same in sql

I am very new in sql, then i am so confused how to get join or get value from two.
First table:
ID P_ID Name AGE U_ID
1 5 B 8 5w
2 8 D 17 6j
3 7 R 67 0qw
Second Table:
ID P_ID Address Edu
1 6 Bddd +2
2 7 Dssss Bachelor
3 2 rress Phd
Here, i want to get accorading to P_ID, but i have U_ID only.
For this: Let us assume that now I have U_ID=0qw.
How to get value from second table. Address and edu , and Age Thanks in advance.
Join on the column that both tables have in common.
select t1.age, t2.address, t2.edu
from table1 t1
join table2 t2 on t1.p_id = t2.p_id
where t1.u_id = '0qw'
Then use the table names or alias names (like t1 for table1) to pick columns from the tables you join.
I think you are looking forward to this:
SELECT t2.Address, t2.Edu, t1.Age
FROM firstTable t1
JOIN secondTable t2
ON t1.P_ID = t2.P_ID
WHERE t1.U_ID = '0qw'
SELECT table1.AGE
, table2.Address
, table2.Edu
FROM table1
INNER JOIN table2 ON (table1.P_ID = table2.P_ID)
WHERE table1.U_ID = '0qw';
NOTE: SQL query is not case sensitive.