我有以下查询。
SELECT DISTINCT propertylist.propertyid
,propertylist.price
,propertylist.publicremarks
,address.addressline1
,address.streetaddress
,address.city
,address.postalcode
,alternateurl.maplink
,building.bathroomtotal
,building.bedroomtotal
,building.constructeddate
,building.sizeinterior
,building.type
,building.basementfeatures
,building.basementtype
,building.constructionstyleattachment
,propertylist.ammenitiesnearby
,propertylist.features
,propertylist.transactiontype
,propertylist.lastupdated
,propertylist.communityfeatures
,land.acreage
FROM propertylist
,address
,building
,alternateurl
,land
WHERE propertylist.propertyid = address.propertyid
AND address.propertyid = building.propertyid
AND building.propertyid = alternateurl.propertyid
AND alternateurl.propertyid = land.propertyid
我想知道将从此查询派生的记录总数,以便我可以在我的网站中实现分页。如果我尝试在没有' 限制'的情况下执行此操作它花了很多时间,执行时间用完了。 Explain sql的结果是
Generation Time: Feb 21, 2015 at 01:06 PM
Generated by: phpMyAdmin 4.2.7.1 / MySQL 5.5.39
SQL query: EXPLAIN SELECT DISTINCT COUNT(*) FROM propertylist , address , building , alternateurl ,land WHERE propertylist.propertyid = address.propertyid AND address.propertyid = building.propertyid AND building.propertyid = alternateurl.propertyid AND alternateurl.propertyid = land.propertyid;
Rows: 5
Current selection does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available.
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE alternateurl ALL NULL NULL NULL NULL 12947
1 SIMPLE address ALL NULL NULL NULL NULL 13338 Using where; Using join buffer
1 SIMPLE building ALL NULL NULL NULL NULL 13389 Using where; Using join buffer
1 SIMPLE propertylist ALL NULL NULL NULL NULL 13614 Using where; Using join buffer
1 SIMPLE land ALL NULL NULL NULL NULL 13851 Using where; Using join buffer
答案 0 :(得分:0)
您可以使用COUNT
修改查询:
SELECT DISTINCT COUNT(*)
FROM propertylist , address , building , alternateurl ,land
WHERE propertylist.propertyid = address.propertyid
AND address.propertyid = building.propertyid
AND building.propertyid = alternateurl.propertyid
AND alternateurl.propertyid = land.propertyid
答案 1 :(得分:0)
要获取查询计数,您应使用count
。
所以你的查询将是
SELECT count (DISTINCT propertylist.propertyid
,propertylist.price
,propertylist.publicremarks
,address.addressline1
,address.streetaddress
,address.city
,address.postalcode
,alternateurl.maplink
,building.bathroomtotal
,building.bedroomtotal
,building.constructeddate
,building.sizeinterior
,building.type
,building.basementfeatures
,building.basementtype
,building.constructionstyleattachment
,propertylist.ammenitiesnearby
,propertylist.features
,propertylist.transactiontype
,propertylist.lastupdated
,propertylist.communityfeatures
,land.acreage )
FROM propertylist
,address
,building
,alternateurl
,land
WHERE propertylist.propertyid = address.propertyid
AND address.propertyid = building.propertyid
AND building.propertyid = alternateurl.propertyid
AND alternateurl.propertyid = land.propertyid
简单
SELECT COUNT(DISTINCT column_name) FROM table_name;
您还应该参考here
答案 2 :(得分:0)
我在sqlserver端做的是将pageno和pagesize作为参数,然后用rowno
创建cte,并在执行时得到总记录数。
也尽可能使用条件连接来过滤数据。并且还将表的始终别名赋予可读和可维护的查询。
检查此示例,只需在我编写时更改列
declare @pageno int , @pagesize int
;With cte
as
(
SELECT ROW_NUMBER() OVER ( order by propertyid or giveyourColumnidToshort ) as rowID, * FROM (
SELECT DISTINCT propertylist.propertyid
,propertylist.price
,propertylist.publicremarks
,address.addressline1
,address.streetaddress
,address.city
,address.postalcode
,alternateurl.maplink
,building.bathroomtotal
,building.bedroomtotal
,building.constructeddate
,building.sizeinterior
,building.type
,building.basementfeatures
,building.basementtype
,building.constructionstyleattachment
,propertylist.ammenitiesnearby
,propertylist.features
,propertylist.transactiontype
,propertylist.lastupdated
,propertylist.communityfeatures
,land.acreage
FROM propertylist as pl
JOIN address as a on pl.propertyid = a.propertyid
join building as b on a.propertyid = b.propertyid
join alternateurl as an on b.propertyid = an.propertyid
join land as l on an.propertyid = l.propertyid --you have to join more data if you have
)
tt
)
SELECT *, noofRows= (SELECT count(propertyid or giveyourColumnidToshort) FROM CTE)
FROM CTE WHERE rowID >= @pageno AND ((@pageno = -1) or rowID<= @pageno)
答案 3 :(得分:0)
在查询中查看SQL_CALC_FOUND_ROWS
和LIMIT
。您需要第二个查询才能获得总结果,但这似乎是处理分页的最简单方法。
https://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows