如何从一张 table 中获取图片数量和封面图片?
How to get number of pics and cover pics from one table?
我正在使用 Apache phoenix。我有 table 张看起来像这样的图片:
|---------------|------------------|-----------|----------|--------|
| id | picture | is_cover | userID | album |
|---------------|------------------|-----------|----------|--------|
| 1 | aaa | true | 1 | test |
|---------------|------------------|-----------|----------|--------|
| 2 | bbb | false | 1 | test |
|---------------|------------------|-----------|----------|--------|
| 3 | ccc | false | 1 | test1 |
|---------------|------------------|-----------|----------|--------|
| 4 | ddd | true | 1 | test1 |
|---------------|------------------|-----------|----------|--------|
我想获取特定用户的相册名称、相册中图片的数量以及相册的封面图片。输出应如下所示:
|---------------|------------------|-----------|
| picture | album | count |
|---------------|------------------|-----------|
| aaa | test | 2 |
|---------------|------------------|-----------|
| ddd | test1 | 2 |
|---------------|------------------|-----------|
你可以尝试使用 join
select t1.picture,t1.album,t2.cnt from
(
select * from pictures where is_cover=true
) t1
join
( select album, count(*) as cnt from pictures
group by album
) t2 on t1.album=t2.album
我正在使用 Apache phoenix。我有 table 张看起来像这样的图片:
|---------------|------------------|-----------|----------|--------|
| id | picture | is_cover | userID | album |
|---------------|------------------|-----------|----------|--------|
| 1 | aaa | true | 1 | test |
|---------------|------------------|-----------|----------|--------|
| 2 | bbb | false | 1 | test |
|---------------|------------------|-----------|----------|--------|
| 3 | ccc | false | 1 | test1 |
|---------------|------------------|-----------|----------|--------|
| 4 | ddd | true | 1 | test1 |
|---------------|------------------|-----------|----------|--------|
我想获取特定用户的相册名称、相册中图片的数量以及相册的封面图片。输出应如下所示:
|---------------|------------------|-----------|
| picture | album | count |
|---------------|------------------|-----------|
| aaa | test | 2 |
|---------------|------------------|-----------|
| ddd | test1 | 2 |
|---------------|------------------|-----------|
你可以尝试使用 join
select t1.picture,t1.album,t2.cnt from
(
select * from pictures where is_cover=true
) t1
join
( select album, count(*) as cnt from pictures
group by album
) t2 on t1.album=t2.album