我想要对3个表进行查询,这将给出如下结果:
我USERID
取决于此我想从下表中获取数据:
ANSTABLE
ID | ANS | USERID | QUERYID
1 | 123 | 1 | 15
2 | 22 | 0 | 16
3 | 17 | 1 | 10
来自ANSTABLE的ID与ANSID在ANSVOTABLE
中映射 ANSVOTABLE
USERID | QUERYID | ANSID | UPVOTE | DOWNVOTE
3 | 15 | 1 | 1 | 0
8 | 15 | 1 | 0 | -1
7 | 15 | 1 | 0 | -1
22 | 16 | 2 | 1 | 0
QUERYID id使用“QUERYTABLE”
中的ID进行映射 QUERYTABLE
ID | USERID | QUERY | DESCRIPTION
16 | 10 | qwerty | uytrew
15 | 11 | my_data | test_data
10 | 0 | 101010 | 101010
现在我想要一个结果,只要我给USERID - 0或1等。
它应该从ANSTABLE
获取与输入的USERID相关的所有queryID
并且根据匹配的queryID,所有记录都应该从QUERYTABLE
表返回,并且它应该给我SUM(UPVOTE)+SUM(DOWNVOTE) as total
对应ANSID&&来自ANSVOTABLE
的QUERYID。
实际上我想要下面的结果 如果我输入`userID - 1,它应该通过添加一个列
给我 ID | USERID | QUERY | DESCRIPTION | ANSSCORE
15 | 11 | my_data | test_data | -1
10 | 0 | 101010 | 101010 | 0
请注意,ANSSCORE通过计算upvote和downvote从-1
的结果变为ANSVOTABLE
。
还要考虑ANSVOTABLE
中是否存在相应的AnsID和QueryId,那么在这种情况下它应该从Querytable返回我的记录,其中o作为总分
答案将受到关注。
答案 0 :(得分:1)
您只需将两列相加,然后按所需的其他列进行分组。您已经定义了表关系,所以这应该是相当简单的,除非我误解了某些内容。
create table anstable(
ID number,
ANS number,
USERID number,
QUERYID number);
Insert into anstable values(1, 123, 1, 15);
Insert into anstable values(2, 22, 0, 16);
Insert into anstable values(3, 17, 1, 10);
create table ANSVOTABLE(
USERID number,
QUERYID number,
ANSID number,
UPVOTE number,
DOWNVOTE number);
Insert into ansvotable values(3, 15, 1, 1, 0);
Insert into ansvotable values(8, 15, 1, 0, -1);
Insert into ansvotable values(7, 15, 1, 0, -1);
Insert into ansvotable values(22, 16, 2, 1, 0);
create table QUERYTABLE(
ID number,
USERID number,
QUERY varchar2(50),
DESCRIPTION varchar2(50));
insert into querytable values(16, 10, 'qwerty', 'uytrew');
insert into querytable values(15, 11, 'my_data', 'test_data');
insert into querytable values(10, 0, '101010', '101010');
select a.id, a.userid, a.query, a.description, sum(b.upvote + b.downvote) as ansscore
from querytable a
join ansvotable b on b.queryid = a.id
join anstable c on c.id = b.ansid
where c.userid in (0, 1)
group by a.id, a.userid, a.query, a.description;
结果:
Table created.
1 row(s) inserted.
1 row(s) inserted.
1 row(s) inserted.
Table created.
1 row(s) inserted.
1 row(s) inserted.
1 row(s) inserted.
1 row(s) inserted.
Table created.
1 row(s) inserted.
1 row(s) inserted.
1 row(s) inserted.
Result Set 1
ID USERID QUERY DESCRIPTION ANSSCORE
15 11 my_data test_data -1
16 10 qwerty uytrew 1
答案 1 :(得分:1)
您需要template < typename T >
class Queue
{
public:
virtual void push( const T& t ) = 0;
};
template < typename Q >
class QueueImpl : public Queue< typename Q::value_type >
{
public:
typedef typename Q::value_type value_type;
virtual void push( const value_type& t ) override
{
q.push( t );
}
private:
Q q;
};
std::unique_ptr< Queue< int > > queue = std::make_unique< QueueImpl< std::queue< int > > >();
queue->push( 1 );
join
上的这3个表,然后根据您的要求keys
执行2或3度group by
。
我认为这可能适合你 -
where clause