I have this table in mysql
1. Is it possible to select a count of - ALL same entity_id where field_tags_tid=2 and field_tags_tid=7
in this example the result would be 1 because only entity_id=6 matches field_tags_tid=2 and field_tags_tid=7
This problem is often called Relational Division
SELECT entity_ID
FROM tableName
WHERE field_tags_ID IN (2,7)
GROUP BY entity_ID
HAVING COUNT(*) = 2
if uniqueness was not enforce on field_tags_ID for every entity_ID then a DISTINCT keyword is needed. otherwise, leave it as is,
SELECT entity_ID
FROM tableName
WHERE field_tags_ID IN (2,7)
GROUP BY entity_ID
HAVING COUNT(DISTINCT field_tags_ID) = 2
SQLFiddle Demo
SQL of Relational Division
UPDATE 1
SELECT COUNT(*) totalCOunt
FROM
(
SELECT entity_ID
FROM tableName
WHERE field_tags_tid IN (2,7)
GROUP BY entity_ID
HAVING COUNT(DISTINCT field_tags_tid) = 2
) s
SQLFiddle Demo
Related
I have mysql table like this
I want to get row that has minimum 2 or more than 2 (multiple) row only from this table, so the result would be like this
What do i do?
thank you
Use GROUP BY and HAVING clauses
SELECT t.* FROM my_table t
JOIN (
SELECT cust_id, MIN(transaction_no) AS transaction_no
FROM my_table
GROUP BY cust_id
HAVING COUNT(cust_id) > 1
) agg ON t.transaction_no = agg.transaction_no
I have table
users|visits
--------------
user1|visit1
user1|visit2
user1|visit3
user2|visit1
user2|visit2
I want to get numbered rows in any group. How i can do this?
users|visits|visit number
-----------------------
user1|visit1|1
user1|visit2|2
user1|visit3|3
user2|visit1|1
user2|visit2|2
Try as below :
SELECT
users, visits, count(*) as `visit number`
FROM tableName group by visits,users
From what you have provide about your problem i can imagine that you have records something like this
users|visits
------------
user1|visit1
user1|visit2
user1|visit1
user1|visit3
And you want to get
users|visits|visit number
-------------------------
user1|visit1|2
user1|visit2|1
user1|visit3|1
To do that use query like this
SELECT
DISTINCT a.users,
a.visits,
(select count(*) from tableName b where b.users = a.users and b.visits = a.visits) as `visit number`
FROM tableName a
i could not create the correct query.
This is the screenshot from the table
I am trying to count only the unique question_id for each cat_id. SO the output must be
totalQuestion cat_id
2 2
2 1
we can use GROUP BY and get distinct count for the questions
SELECT COUNT(DISTINCT question_id) as total_questions, cat_id
from tableA
group by cat_id
select
count(distinct qid) as totalQuestion
, cid
from table_name group by cid
I have table with, folowing structure.
tbl
id name
1 AAA
2 BBB
3 BBB
4 BBB
5 AAA
6 CCC
select count(name) c from tbl
group by name having c >1
The query returning this result:
AAA(2) duplicate
BBB(3) duplicate
CCC(1) not duplicate
The names who are duplicates as AAA and BBB. The final result, who I want is count of this duplicate records.
Result should be like this:
Total duplicate products (2)
The approach is to have a nested query that has one line per duplicate, and an outer query returning just the count of the results of the inner query.
SELECT count(*) AS duplicate_count
FROM (
SELECT name FROM tbl
GROUP BY name HAVING COUNT(name) > 1
) AS t
Use IF statement to get your desired output:
SELECT name, COUNT(*) AS times, IF (COUNT(*)>1,"duplicated", "not duplicated") AS duplicated FROM <MY_TABLE> GROUP BY name
Output:
AAA 2 duplicated
BBB 3 duplicated
CCC 1 not duplicated
For List:
SELECT COUNT(`name`) AS adet, name
FROM `tbl` WHERE `status`=1 GROUP BY `name`
ORDER BY `adet` DESC
For Total Count:
SELECT COUNT(*) AS Total
FROM (SELECT COUNT(name) AS cou FROM tbl GROUP BY name HAVING cou>1 ) AS virtual_tbl
// Total: 5
why not just wrap this in a sub-query:
SELECT Count(*) TotalDups
FROM
(
select Name, Count(*)
from yourTable
group by name
having Count(*) > 1
) x
See SQL Fiddle with Demo
The accepted answer counts the number of rows that have duplicates, not the amount of duplicates. If you want to count the actual number of duplicates, use this:
SELECT COALESCE(SUM(rows) - count(1), 0) as dupes FROM(
SELECT COUNT(1) as rows
FROM `yourtable`
GROUP BY `name`
HAVING rows > 1
) x
What this does is total the duplicates in the group by, but then subtracts the amount of records that have duplicates. The reason is the group by total is not all duplicates, one record of each of those groupings is the unique row.
Fiddle: http://sqlfiddle.com/#!2/29639a/3
SQL code is:
SELECT VERSION_ID, PROJECT_ID, VERSION_NO, COUNT(VERSION_NO) AS dup_cnt
FROM MOVEMENTS
GROUP BY VERSION_NO
HAVING (dup_cnt > 1 && PROJECT_ID = 11660)
I'm using this query for my own table in PHP, but it only gives me one result whereas I'd like to the amount of duplicate per username, is that possible?
SELECT count(*) AS duplicate_count
FROM (
SELECT username FROM login_history
GROUP BY username HAVING COUNT(time) > 1
) AS t;
I have a flags_products table like below (many to many relationship between flags and products),
flags_products :
flag_id -- product_id
1 -- 1
2 -- 1
3 -- 1
1 -- 2
2 -- 2
4 -- 2
What is the SQL query to get rows that have both the flags (flag_id's 1 and 2) associated with a product (product_id)? Obviously:
SELECT *
FROM flags_products
WHERE flag_id = 1
AND flag_id = 2
GROUP BY product_id;
...doesn't work, and gives an empty set. So what would be the correct query?
The exact approach that you should take will depend on your larger requirements. Here's one possible solution:
SELECT
product_id
FROM
Some_Table
WHERE
flag_id IN (1, 2)
GROUP BY
product_id
HAVING
COUNT(*) = 2
This works as long as you can't have duplicates. If a product can be flagged twice with the same flag_id then you would need:
SELECT
product_id
FROM
Some_Table
WHERE
flag_id IN (1, 2)
GROUP BY
product_id
HAVING
COUNT(DISTINCT flag_id) = 2
In both of these cases you'll need the GROUP BY to match your column list. MySQL doesn't require that in order for the query to run (a flaw IMO, but I'll save that argument for another time ), but the results won't be determinable for the columns not in the GROUP BY. You can also use the above to queries as subqueries which you can then join to another table or tables.
If you know that it will always be exactly two flags for which you're looking then you can use EXISTS:
SELECT
T1.product_id
FROM
Some_Table T1
WHERE
EXISTS (
SELECT *
FROM
Some_Table T2
WHERE
T2.product_id = T1.product_id AND
T2.flag_id = 1
) AND
EXISTS (
SELECT *
FROM
Some_Table T2
WHERE
T2.product_id = T1.product_id AND
T2.flag_id = 2
)
Performance may not be very good though.
You can use INTERSECT operator.
SELECT product_id
FROM t
WHERE flag_id = 1
INTERSECT
SELECT product_id
FROM t
WHERE flag_id = 2
This query will return all product_id which have both flag_id = 1 or 2