查看: 954|回复: 9
|
dropdownlist 里疑问!! (asp.net)
[复制链接]
|
|
我的database structure (ID, property_type)
1 , home (double storey)
2 , home (double storey)
3 , flat
4 , shop
5 , home (single storey)
6 , shop
7 , flat
我要如何利用sql statement来计算出quantity of various property type, 然后再display在dropdownlist?? 如下 :
dropdownlist:
home (3)
shop (2)
flat (2)
以下我目前我使用的code.. 但只能显示出property type而已
Public Function retrieve_property_type() As DataSet
Dim mydataadapter As OdbcDataAdapter
Dim mydataset As DataSet
Str = "SELECT type FROM property"
Try
mydataadapter = New OdbcDataAdapter(Str, MyConn)
mydataset = New DataSet
mydataadapter.Fill(mydataset, "property_type_table")
Catch ex As Exception
End Try
If Not MyConn Is Nothing Then
MyConn.Close()
End If
Return mydataset
End Function |
|
|
|
|
|
|
|
发表于 2-6-2006 12:47 PM
|
显示全部楼层
SELECT property_type, COUNT(property_type) FROM property GROUP BY property_type |
|
|
|
|
|
|
|

楼主 |
发表于 2-6-2006 01:13 PM
|
显示全部楼层
但这样做也只能显示出 property type而已, 没有quantity |
|
|
|
|
|
|
|
发表于 2-6-2006 01:21 PM
|
显示全部楼层
原帖由 counterking 于 2-6-2006 01:13 PM 发表
但这样做也只能显示出 property type而已, 没有quantity
你没仔细看我的 sql, 请至少先尝试. |
|
|
|
|
|
|
|

楼主 |
发表于 4-6-2006 03:39 PM
|
显示全部楼层
goatstudio 大兄,我并不是没有尝试。因为我也不想太依赖你们。。
以下是我的code for add items to dropdownlist...
--------------------------------------------------------------------
Private Function generate_property_type()
Dim dataset As New DataSet
Dim x As Integer
If ddl_Property_type.Items.Count = 0 Then
dataset = log.retrieve_property_type()
ddl_Property_type.Items.Add("property_type")
If dataset.Tables("property_type_table").Rows.Count <> 0 Then
While x < dataset.Tables("property_type_table").Rows.Count
ddl_Property_type.Items.Add(dataset.Tables("property_type_table").Rows(x).ItemArray(0))
x = x + 1
End While
End If
End If
End Function
------------------------------------------------------------------------
Public Function retrieve_property_type() As DataSet
Dim mydataadapter As OdbcDataAdapter
Dim mydataset As DataSet
Str = "SELECT property_type, COUNT(property_type) FROM property GROUP BY property_type"
Try
mydataadapter = New OdbcDataAdapter(Str, MyConn)
mydataset = New DataSet
mydataadapter.Fill(mydataset, "property_type_table")
Catch ex As Exception
End Try
If Not MyConn Is Nothing Then
MyConn.Close()
End If
Return mydataset
End Function |
|
|
|
|
|
|
|
发表于 4-6-2006 03:52 PM
|
显示全部楼层
其实很简单... quantity 已经在那里... 你没看到...
如果你这样写...
Public Function retrieve_property_type() As DataSet
Dim mydataadapter As OdbcDataAdapter
Dim mydataset As DataSet
Str = "SELECT property_type, COUNT(property_type) AS Quantity FROM property GROUP BY property_type"
Try
mydataadapter = New OdbcDataAdapter(Str, MyConn)
mydataset = New DataSet
mydataadapter.Fill(mydataset, "property_type_table")
Catch ex As Exception
End Try
If Not MyConn Is Nothing Then
MyConn.Close()
End If
Return mydataset
End Function
Private Function generate_property_type()
Dim dataset As New DataSet
Dim x As Integer
If ddl_Property_type.Items.Count = 0 Then
dataset = log.retrieve_property_type()
ddl_Property_type.Items.Add("property_type")
If dataset.Tables("property_type_table").Rows.Count <> 0 Then
While x < dataset.Tables("property_type_table").Rows.Count
ddl_Property_type.Items.Add(dataset.Tables("property_type_table").Rows(x).ItemArray(0) & " (" & dataset.Tables("property_type_table").Rows(x).ItemArray(1) & ") ")
x = x + 1
End While
End If
End If
End Function
在现实工作中, 你不能跟着指示书本一成不变... 需要懂得运用 code 来变通...
其实问题很简单... 只是看你如何去解决... |
|
|
|
|
|
|
|

楼主 |
发表于 5-6-2006 05:37 PM
|
显示全部楼层
之前goatstudio用以下的code帮我解决我的问题。。。
SELECT property_type, COUNT(property_type) FROM property GROUP BY property_type
新的问题 :
my database items (id, property_type )
1 , house - double storey
2 , flat
3 , flat
4 , house - single storey
5 , shop
6 , shop
7 , house - corner slot
8 , house - single storey
sql 我该如何写才能出现这样的数据
shop (2)
flat (2)
house (4)
其实重点就是在于如何让[house - single storey] and [house - double storey]等变成[house]..
不好意思, 因为一些缘故,我不能修改这个database's structure and the way how the data stored
|
|
|
|
|
|
|
|
发表于 5-6-2006 08:52 PM
|
显示全部楼层
原帖由 counterking 于 5-6-2006 05:37 PM 发表
之前goatstudio用以下的code帮我解决我的问题。。。
SELECT property_type, COUNT(property_type) FROM property GROUP BY property_type
新的问题 :
my database items (id, property_type )
...
简单的很, 就在 sql statement 那里改改就行了... 你可以用 UNION 来达到...
别老是被限制在简单的 select 里, 应该 think out of the box.  |
|
|
|
|
|
|
|

楼主 |
发表于 6-6-2006 08:30 PM
|
显示全部楼层
如果我没有会错意,union的用法应该是从两个不同的table搜寻出资料...
SELECT E_Name FROM Employees_Norway
UNION
SELECT E_Name FROM Employees_USA
我想了很久,也不明白如何利用union来达至我的需求。。。
property table (ID, property_type)
1 , house (double stoley)
2 , house (double stoley)
3 , flat
4 , apartment
5 , flat
6 , house (single stoley)
7 , house (cornet slot)
现在的搜寻结果是这样:
house (double stoley) (2)
house (single stoley) (1)
house (cornet slot) (1)
flat (2)
apartment (1)
但我想要将搜寻结果house (double stoley), house (single stoley), house (cornet slot)
全部以 house 来表示... 如下:
apartment (1)
flat (2)
house (4)
[ 本帖最后由 counterking 于 7-6-2006 10:21 AM 编辑 ] |
|
|
|
|
|
|
|
发表于 7-6-2006 10:03 PM
|
显示全部楼层
你想得还不够... 懂得 UNION 的用途没有用, 还要懂得如何去运用...
SELECT property_type, COUNT(property_type) AS Quantity FROM property WHERE property_type NOT LIKE 'house%' GROUP BY property_type
UNION
SELECT 'house', (SELECT COUNT(*) FROM property phouse WHERE phouse.property_type LIKE 'house%') AS Quantity FROM property
加油吧, 别死读书, 要活学活用.  |
|
|
|
|
|
|
| |
本周最热论坛帖子
|