Counting MySQL GROUP BY - php

So, I have next table structure :
Is there a way to make SQL query that counts simillar hashes in r_hash column, than founds this hash in hash column and returns an uid, and count of hashes?
For example - uid - 21520578; type - 1; count - 7?

Try the below query
SELECT T1.uid, T1.type, T2.count
FROM table T1,
(
SELECT r_hash, COUNT(*) AS count
FROM table
GROUP BY r_hash
) T2
WHERE T1.hash = T2.r_hash

You can do that by using join
SELECT t1.uid, t1.type, COUNT(t2.id) as `count`
FROM table AS t1
LEFT JOIN table AS t2 ON t2.r_hash = t1.hash
GROUP BY t1.id
I am not tested this query.
Edit: with left join you also receive rows with count = 0.

Related

Getting multi count from two table and save it in one variable in PHP Codeigniter

I have two tables and I need to get the count of these tables for a condition?
table 1: "tbl_comment" table 2: "tbl_group_comment"
both have some common columns which are login_id of the user,
this is the query:
$param="(SELECT COUNT(*) FROM tbl_comment as t2 WHERE login_id=1) as commentCount, (SELECT COUNT(*) FROM tbl_group_comment as t3 WHERE login_id=1) as commentCount";
$table="`tbl_comment` t2 join `tbl_group_comment`";
$save['value']=$this->Common_model->common_join($param,$table);
this one is not working but if I am changing the second commentCount and keep it as commentCount1 it will give me the value of each table, I want to get the sum of both counts?
is there any specific clause for this matter?
Try this:
select sum(comment_count + group_count) from
((SELECT COUNT(*) as comment_count FROM tbl_comment WHERE login_id=1) A,
(SELECT COUNT(*) as group_count FROM tbl_group_comment WHERE login_id=1)B
);

How we can get the data from the table by limiting the number of identical columns MySql

Yesterday I tried to retrieve data from my db table using 'user_id' as a criterion to limit the amount of data per user.
I tried to get data from table https://prnt.sc/p53zhp in format like this https://prnt.sc/p541wk and limit the number of output records for user_id where limit will be 2 (count(user_id) <= 2), but i don't understand how to do that. What kind of sql request can i use to get this data?
Assuming that your RDBMS, here is a solution yo select only the top 2 records per user. You can use ROW_NUMBER() in a subquery to rank records by id within groups of records having the same user_id, and the filter out unerelevant records in the outer query, like:
SELECT *
FROM (
SELECT
t.*,
ROW_NUMBER() OVER(PARTITION BY user_id ORDER BY id)
FROM mytable
) x WHERE rn <= 2
On earlier versions of MySQL, you could use self-LEFT JOIN the table and use GROUP BY and HAVING COUNT(...) < 2 to limit the results to first two records per group:
SELECT
t.id,
t.user_id,
t.vip,
t.title,
t.description,
t.data
FROM mytable t
LEFT JOIN mytable t1 ON t1.user_id = t.user_id AND t1.id > t.id
GROUP BY
t.id,
t.user_id,
t.vip,
t.title,
t.description,
t.data
HAVING COUNT(t1.id) < 2
I don't understand if your problem is a Transact-SQL or your code.
In SQL you can limit record with "LIMIT": https://www.w3schools.com/sql/sql_top.asp
In code, you can use a condition IF.

Problems with inner join SQL

Basically i am working with 2 tables, one is the cars and one is comments and i am trying to get the count of approved comments for certain cars + the cars information, but i would like it to be dynamic if i put in the id of the car directly it will work but when i say where id = car id it returns nothing?
This works with "10" it will give me the information + the count of comments
SELECT t1.biler_id, t1.biler_navn, COUNT(t2.bilbixen_commentary_status)
FROM bilbixen_biler t1
INNER JOIN bilbixen_commentary t2 ON t1.biler_id = t2.bilbixen_commentary_bil
WHERE t1.biler_id = 10
The above query will return this in phpmyadmin
The below query don't work, i am trying to tell it to find everything where t1.id is equal t2.id
SELECT t1.biler_id, t1.biler_navn, COUNT(t2.bilbixen_commentary_status)
FROM bilbixen_biler t1
INNER JOIN bilbixen_commentary t2 ON t1.biler_id = t2.bilbixen_commentary_bil
WHERE t1.biler_id = t2.bilbixen_commentary_bil
The above query will give me this error in phpmyadmin
I expected it to return all the information for each car with a count of comments for each of them
Your where clause is useless and you need a group by clause since you are using the aggregate function count.
select t1.biler_id,
t1.biler_navn,
COUNT(t2.bilbixen_commentary_status)
from bilbixen_biler t1
left join bilbixen_commentary t2 on t1.biler_id = t2.bilbixen_commentary_bil
group by t1.biler_id,
t1.biler_navn
You have used the aggregate function COUNT so you have to group by t1.biler_id, t1.biler_navn fields:
SELECT t1.biler_id, t1.biler_navn, COUNT(t2.bilbixen_commentary_status)
FROM bilbixen_biler t1
INNER JOIN bilbixen_commentary t2 ON t1.biler_id = t2.bilbixen_commentary_bil
GROUP BY t1.biler_id, t1.biler_navn

PHP / MYSQL Statement so select Max Sum of Join

I have Problems with a select statement, as a little help here are the important columns:
Table1
ID NAME
TABLE 2
ID U_ID COUNTER
The ID of Table 1 Matches the U_ID of Table 2. Table 2 contains many entries for the same u_id.
What I want to do is to get the Name of the "user" (table 1) who has in sum the max. counter.
What I got since now is the join of the tables (Where clause depends on other rows which are not important for the problem).
Can anyone help me on this issue?
So what you need is an aggregate of an aggregate (max of sum of column). The easiest will be to create a view providing the sum and u_id end then select the max of it:
create view table2sums
as
select u_id, sum(counter) as total
from table2
group by u_id;
and then
select t1.name
from table1 t1, table2sums t2
where t1.id = t2.u_id
and t2.total >= all (
select total
from table2sums
)
In this special case you can also do it directly:
select t1.name
from table1 t1, table2 t2
where t1.id = t2.u_id
group by t1.name
having sum(t2.counter) >= all (
select sum(counter)
from table2
group by t2.u_id
)
NOTE: The other proposed solutions will show a better performance. My solution only selects the name (which is what you said you wanted) and works in any RDBMS.
There exist RDBMS without the LIMIT possibility.
In the end, I'd say: regard my solution as educational, the others as practical
SELECT name,
SUM(counter) as counter
FROM table1
JOIN table2
ON table1.id = table2.u_id
GROUP BY u_id
ORDER BY counter DESC
LIMIT 1
You can try this:
SELECT name, SUM(counter) as total_counter
FROM table1
JOIN table2
ON table1.id = table2.u_id
GROUP BY u_id
ORDER BY total_counter DESC
LIMIT 1
Working Demo: http://sqlfiddle.com/#!2/45419/4

Select unmatched records from two tables of MYSQL

I have two tables Table1 and Table2 with some records
id is the common column in both tables and primarykey is set to this column in table1
There are many records in table1 and some of these records (not all) are updated into table2.
Now I want retrieve from table1 the records not updated into the table2.
For example in table1 there are records 1,2,3,4,5,6,7,8,9
And in table2 there are 3,4,7,9
Now How can I retrieve these records form table1 1,2,5,6 those not updated into table2
I wrote this query :
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME
FROM [Table1] INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id;
But the expected result not coming. This query lists all the records repeating each one record manytimes
Can any body give me solution to get the expected result.
select *
from table1
where table1.slip_no NOT IN (select id from table2)
Assuming name of common column is id
Or you can modify your query as
SELECT distinct (Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME)
FROM [Table1]
INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id
A good reference on SQL joins
SELECT t1.*
FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2 USING(id)
WHERE
t2.id IS NULL;
You can use the NOT IN operator on a subquery for table2.
Alternatively, use MINUS with two regular queries listing the ids in each table:
SELECT id FROM table1
MINUS
SELECT id FROM table2;
Try this
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME FROM [Table1]
WHERE
NOT EXISTS (SELECT * from Table2 WHERE Table1.SLIPNO !=Table2.id );
You can use the following query
SELECT id FROM database1.table WHERE id NOT IN(SELECT id FROM database2.table)
SELECT child_table.id FROM child_table LEFT JOIN parent_table ON child_table.parent_id = parent_table.id WHERE parent_table.id IS NULL
This left join query returns all the records of the child_table when there is no match in the parent_table. When there is no match, all parent_table fields will be NULL.
inner join will not help. To get unmatched records I tried this:
SELECT
A.ID,A.DATE,A.NAME
FROM TABLE1 A
WHERE CONCAT(A.ID , A.DATE ,A.NAME)
NOT IN
(SELECT CONCAT(B.ID , B.DATE ,B.NAME) as X
from TABLE2 B) ;

Categories