简体   繁体   中英

Count (*) rows based on values from other table

RECORD TABLE

record_id  type_id   title
---------  -------   -----
1          1         Title1
2          2         Title2
3          1         Title3
4          2         Title4

RECORD VALUES

id   record_id   field_id  field_value
--   ---------   --------  -----------
1    1           8         active 
2    2           12        some value
3    1           16        lenght
4    2           3         some value

I want to count records FROM record table which type_id is = 1 AND FROM record values table field_id_8 field value = active AND field_id_16 field value = lenght.

Based on the two example table above the query return 1 ( record_id = 1 is the only one row which fit to query).

I can create simple MYSQL queries but I can't do anything to solve this one. Anyone can help me?

EDIT: OK first time i was misunderstood the question I hope now i figure it right.

If i was right in comment below this should work for you:

SELECT COUNT(DISTINCT(rt.record_id)) AS COUNTED
FROM record_table rt
INNER JOIN record_values rv
ON rt.record_id = rv.record_id
WHERE rt.type_id = 1 AND 
(8 IN (SELECT rv2.field_id
       FROM record_values rv2
       WHERE rv2.record_id = rt.record_id) AND 
'active' IN (SELECT rv2.field_value
             FROM record_values rv2
             WHERE rv2.record_id = rt.record_id)) AND
 (16 IN (SELECT rv2.field_id
         FROM record_values rv2
         WHERE rv2.record_id = rt.record_id) AND
 'lenght' IN (SELECT rv2.field_value
              FROM record_values rv2
              WHERE rv2.record_id = rt.record_id))

Here is sql fiddle for that http://sqlfiddle.com/#!9/7be41/14 so you can check if that's work now...

GL!

One more edit:

When you have data like this:

 +------+-----------+----------+-------------+
 | id   | record_id | field_id | field_value |
 +------+-----------+----------+-------------+
 |  xx  |     7     |    23    |   active    |
 +------+-----------+----------+-------------+
 |  xx  |     7     |    16    |   lenght    |
 +------+-----------+----------+-------------+
 |  xx  |     7     |    8     |  some value |
 +------+-----------+----------+-------------+
 | etc...                                    |

Do you want to that data be included and counted or not. If you want to be use the query from the beginning of the answer. if you don't want to be counted use this query:

SELECT COUNT(DISTINCT(rt.record_id)) AS COUNTED
FROM record_table rt
INNER JOIN record_values rv
ON rt.record_id = rv.record_id
WHERE rt.type_id = 1 AND 
8 IN (SELECT rv2.field_id
      FROM record_values rv2
      WHERE rv2.record_id = rt.record_id AND
      rv2.field_value = 'active') AND
16 IN (SELECT rv2.field_id
       FROM record_values rv2
       WHERE rv2.record_id = rt.record_id AND
       rv2.field_value = 'lenght')

This query will count only record_id which have two rows in record_values table like this:

 +------+-----------+----------+-------------+
 | id   | record_id | field_id | field_value |
 +------+-----------+----------+-------------+
 |  xx  |     y     |    8     |   active    |
 +------+-----------+----------+-------------+
 |  xx  |     y     |    16    |   lenght    |
 +------+-----------+----------+-------------+

Hope this help end you understand the difference between this two query. Here is SQL Fiddle and for this second query so you can play a little with data and see what's happened http://sqlfiddle.com/#!9/c52bf/2

NOTE in second fiddle i change a little data in table to test it so to not be confused...

GL!

ONE MORE EDIT

Ups i just realize that there is a better way to solve this. Hope not too late...

SELECT COUNT(*)
FROM record_values rv
INNER JOIN record_values rv2
ON rv.record_id = rv2.record_id
WHERE rv.field_id = 8 AND rv.field_value = 'active' AND 
      rv2.field_id = 16 AND rv2.field_value = 'lenght'

I think this query is more convenient for your problem...

Here is a SQL Fiddle for that...

NOTE I add few more value there for testing so 2 is a correct answer in that data set... :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM