0

I have the following query.

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

I want to know the total number of records that will be derived from this query so that i can implement paging in my website. If i try to execute this without 'limit' it takes so much time and the execution time runs out. The result of Explain sql is

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
Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89

4 Answers4

0

You can modify your query using 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
shauryachats
  • 9,975
  • 4
  • 35
  • 48
0

To get the count of your query you shall use count.

So your Query will be

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

In Simple

SELECT COUNT(DISTINCT column_name) FROM table_name;

You shall also refer here

Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
  • #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT propertylist.propertyid ,propertylist.price ,propertylist.publi' at line 1 – Sahil Manchanda Feb 21 '15 at 11:24
0

What I did in sqlserver side was give pageno and pagesize as a arguments, then created cte with rowno and while executing got the count of total record.

Also use join with condition as much as possible with to filter data. And also give the always alias of tables to readable and maintainable query.

check this example, just change your column as I written

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)
Ajay2707
  • 5,690
  • 6
  • 40
  • 58
0

Take a look at SQL_CALC_FOUND_ROWS along with a LIMIT on your query. You'd need a second query to get the total results, but this seems to be the easiest way to handle pagination.

https://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows