在SQL中查询n个参数

时间:2016-12-31 11:21:21

标签: sql postgresql

粗略地说,美国农业部的营养数据库结构如下:

+---------+--------+
| food_id |  name  |
+---------+--------+
|       1 | butter |
|       2 | bacon  |
|       3 | eggs   |
+---------+--------+

+---------+-----------+----------------+
| food_id | per_100_g | description_id |
+---------+-----------+----------------+
|       1 |        20 |              1 |
|       1 |        10 |              2 |
|       2 |        30 |              1 |
|       2 |        70 |              2 |
|       3 |        10 |              1 |
|       3 |        80 |              2 |
+---------+-----------+----------------+

+----------------+-------------+
| description_id | description |
+----------------+-------------+
|              1 | fat         |
|              2 | protein     |
+----------------+-------------+

我是编写SQL的新手。我可以编写一个结合所有三个表的连接,我可以根据一个参数查询,例如食品有多少脂肪。但有没有办法查询食物中有多少脂肪per_100_g和多少蛋白质per_100_g?我知道这有点主观,但我希望能从中得到一些。谢谢!

我的数据库是postgres。理想情况下,这是一个可以扩展到n个营养标准和n种组合的解决方案,例如,食物中含有多少脂肪,蛋白质和钠,或者多少维生素c,k和钾 - 没有写几千种查询。

编辑:

我写的一个示例查询,但只发现了单一的营养数据:

select des.long_desc
    from food_des des
    inner join nut_data fat
        on des.ndb_no = fat.ndb_no
    inner join nutr_def fat_des
        on fat.nutr_no = fat_des.nutr_no
    where (fat_des.tagname = 'FAT') and (fat.nutr_val < 10)

编辑2:

我希望胖子少于30且蛋白质大于10的查询结果会返回此过滤结果

+---------+-------+-----------+---------------+
| food_id | name  | fat_value | protein_value |
+---------+-------+-----------+---------------+
|       2 | bacon |        30 |            70 |
|       3 | eggs  |        10 |            80 |
+---------+-------+-----------+---------------+

2 个答案:

答案 0 :(得分:1)

你可以做这样的事情来寻找含有多种营养素的食物:

Having

可以轻松扩展server { listen 80; server_name example.com http://example.com; return 301 https://example.com$request_uri; } server { listen 80; server_name www.example.com http://www.example.com; return 301 https://www.example.com$request_uri; } server { listen 443 ssl http2 default_server; listen [::]:443 ssl http2 default_server; include snippets/ssl-example.com.conf; include snippets/ssl-params.conf; root /home/tim/site.folder; index index.html index.htm index.nginx-debian.html; server_name _; location ~ /.well-known { allow all; } location / { try_files $uri $uri/ =404; } } 部分以支持更多过滤器。

答案 1 :(得分:1)

也许是这样的:

select
    f.name, 
    j.fat_per_100_g,
    j.protein_per_100_g
from 
    foods f
    inner join (
        select
            i.food_id,
            sum(case when d.description = 'fat' then
                i.per_100_g else null end) as fat_per_100_g,
            sum(case when d.description = 'protein' then
                i.per_100_g else null end) as protein_per_100_g
        from
            ingredients i
            inner join descriptions d
            on i.description_id = d.description_id
        group by
            i.food_id
        ) j
    on f.food_id = j.food_id
where
    j.fat_per_100_g < 30 and
    j.protein_per_100_g > 10
;

    name    | fat_per_100_g | protein_per_100_g 
------------+---------------+-------------------
 eggs       |            10 |                80

P.S。培根有30脂肪_per_100_g所以如果脂肪不得超过30克,它就不会被退回。