由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - 请教一个query in mysql
相关主题
请教怎么来log duration of a MYSQL procedure?Linux server, mysql
有包子!sql procedure 来rank不同table里面的数据有用phpMyadmin的吗,请教
问个问题a complex sql query, high hand help!!!
这个query对么?如何完成这个sql?
sql questionmysql索引/优化的一个问题
ask for help请教一个SQL Query
请问如何实现这样一个mysql的query, 谢谢== MySql问题 ==
请问这个query怎么做我也问一个sql querry的问题
相关话题的讨论汇总
话题: table话题: column1话题: query话题: column3话题: column2
进入Database版参与讨论
1 (共1页)
g*********e
发帖数: 29
1
Table A:
column1 column2 column3
a x 2
a c 4
a x 1
b d 9
c f 7
c j 3
请问怎样加index based on column1 to create table B:
index column1 column2 column3
1 a x 2
2 a c 4
3 a x 1
1 b d 9
1 c f 7
2 c j 3

我想这样我就可以select * from table B where index=2
2 a c 4
2 c j 3
s*******u
发帖数: 19
2
It seems it does not make sense to create another table for this query if
the data is dynamic in Table A. Moreoevr, it loses the business logic
meaning by querying table B in your way even you can do this.
g*********e
发帖数: 29
3
Yes, probably it doesn't make sense here. But I really want to do is:
there are many accounts in the database, each account has made a different
payments along with the payment date.
I want to see the first pay of each account, the second pay of each account.
.....
It's easy to see the first pay of each account, for example,
select accountNum, sum(amount),min(paymentdate) from tablename group by
accountNum (it's ok?)
But how to query the second, third pay...makes me headache. That's why I
post t
t**n
发帖数: 30
4
Can you query while sort it by date?

【在 g*********e 的大作中提到】
: Table A:
: column1 column2 column3
: a x 2
: a c 4
: a x 1
: b d 9
: c f 7
: c j 3
: 请问怎样加index based on column1 to create table B:
: index column1 column2 column3

c*****t
发帖数: 1879
5
how about LIMIT and OFFSET.

account.

【在 g*********e 的大作中提到】
: Yes, probably it doesn't make sense here. But I really want to do is:
: there are many accounts in the database, each account has made a different
: payments along with the payment date.
: I want to see the first pay of each account, the second pay of each account.
: .....
: It's easy to see the first pay of each account, for example,
: select accountNum, sum(amount),min(paymentdate) from tablename group by
: accountNum (it's ok?)
: But how to query the second, third pay...makes me headache. That's why I
: post t

s*******u
发帖数: 19
6
You may try to modify this:
drop table if exists A;
create table A (c1 char(1), c2 char(2), c3 int);
insert into A values ('a', 'x', 0), ('a', 'c', 4), ('a', 'x', 1),('b', 'd', 9),('c', 'f', 7),('c', 'j', 3);
insert into A values ('a', 'd', 10);
SELECT IF(@last=c1, @rank:= @rank + 1,@rank:=1) as rank,@last:=c1 as c11, c3, c2
FROM A, (SELECT @rank:=1, @last:='0') x ORDER BY c1, c3;
k********e
发帖数: 702
7
It's easy to see the first pay of each account, for example,
select accountNum, sum(amount),min(paymentdate) from tablename group by
accountNum (it's ok?)
g*********e
发帖数: 29
8
Thanks for the reply. Seattlewu's method is great but the real data is huge,
hence, we cannot insert data manually; Kissbigeye's idea is practical,
however, I am just going to query from one table.
This table includes thousands of accounts and other variables.
The table looks like:
AccountNum amount paymentdate .......
a xx 04/01/08
a xx 04/05/08
c xx 04/05/08
a xx 04/09/08
.
.
.
I want to get the query 1
s*******u
发帖数: 19
9
The 'tablename' in Kissbigeye's query is the same table. This is called self
-join -- you probably should try a little more to learn SQL.

huge,

【在 g*********e 的大作中提到】
: Thanks for the reply. Seattlewu's method is great but the real data is huge,
: hence, we cannot insert data manually; Kissbigeye's idea is practical,
: however, I am just going to query from one table.
: This table includes thousands of accounts and other variables.
: The table looks like:
: AccountNum amount paymentdate .......
: a xx 04/01/08
: a xx 04/05/08
: c xx 04/05/08
: a xx 04/09/08

g*********e
发帖数: 29
10
Wonderful! Many thanks to seattlewu and kissbigeye, you are awesome.
1 (共1页)
进入Database版参与讨论
相关主题
我也问一个sql querry的问题sql question
recursive query helpask for help
请帮我看看,什么地方错了?请问如何实现这样一个mysql的query, 谢谢
需要帮助 -- Oracle Query请问这个query怎么做
请教怎么来log duration of a MYSQL procedure?Linux server, mysql
有包子!sql procedure 来rank不同table里面的数据有用phpMyadmin的吗,请教
问个问题a complex sql query, high hand help!!!
这个query对么?如何完成这个sql?
相关话题的讨论汇总
话题: table话题: column1话题: query话题: column3话题: column2