WatsMyName
WatsMyName

Reputation: 4478

fetching records with long sql query with multple joins

I will try to explain things as much as I can. I have following query to fetch records from different tables.

SELECT 
  p.p_name,
  p.id,
  cat.cat_name,
  p.property_type,
  p.p_type,
  p.address,
  c.client_name,
  p.price,
  GROUP_CONCAT(pr.price) AS c_price,
  pd.land_area,
  pd.land_area_rp,
  p.tagline,
  p.map_location,
  r.id,
  p.status,
  co.country_name,
  p.`show`,
  u.name,
  p.created_date,
  p.updated_dt,
  o.type_id,
  p.furnished,
  p.expiry_date 
FROM
  property p 
  LEFT OUTER JOIN region AS r 
    ON p.district_id = r.id 
  LEFT OUTER JOIN country AS co 
    ON p.country_id = co.country_id 
  LEFT OUTER JOIN property_category AS cat 
    ON p.cat_id = cat.id 
  LEFT OUTER JOIN property_area_details AS pd 
    ON p.id = pd.property_id 
  LEFT OUTER JOIN sc_clients AS c 
    ON p.client_id = c.client_id 
  LEFT OUTER JOIN admin AS u 
    ON p.adminid = u.id 
  LEFT OUTER JOIN sc_property_orientation_type AS o 
    ON p.orientation_type = o.type_id 
  LEFT OUTER JOIN property_amenities_details AS pad 
    ON p.id = pad.property_id 
  LEFT OUTER JOIN sc_commercial_property_price AS pr 
    ON p.id = pr.property_id 
WHERE p.id > 0 
  AND (
    p.created_date > DATE_SUB(NOW(), INTERVAL 1 YEAR) 
    OR p.updated_dt > DATE_SUB(NOW(), INTERVAL 1 YEAR)
  ) 
  AND p.p_type = 'sale' 

everything works fine if I exclude GROUP_CONCAT(pr.price) AS c_price, from above query. But when I include this it just gives one result. My intention to use group concat above is to fetch comma separated price from table sc_commercial_property_price that matches the property id in this case p.id. If the records for property exist in sc_commercial_property_price then fetch them in comma separated form along with other records. If not it should return blank. What m I doing wrong here?

I will try to explain again if my problem is not clear. Thanks in advance

Upvotes: 0

Views: 86

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269753

The GROUP_CONCAT is an aggregation function. When you include it, you are telling SQL that there is an aggregation. Without a GROUP BY, only one row is returns, as in:

select count(*)
from table

The query that you have is acceptable syntax in MySQL but not in any other database. The query does not automatically group by the columns with no functions. Instead, it returns an arbitrary value. You could imagine a function ANY, so you query is:

select any(p.p_name) as p_num, any(p.tagline) as tagline, . . .

To fix this, put all your current variables in a group by clause:

GROUP BY
  p.p_name,
  p.id,
  cat.cat_name,
  p.property_type,
  p.p_type,
  p.address,
  c.client_name,
  p.price,
  pd.land_area,
  pd.land_area_rp,
  p.tagline,
  p.map_location,
  r.id,
  p.status,
  co.country_name,
  p.`show`,
  u.name,
  p.created_date,
  p.updated_dt,
  o.type_id,
  p.furnished,
  p.expiry_date 

Most people who write SQL think it is good form to include all the group by variables in the group by clause, even though MySQL does not necessarily require this.

Upvotes: 1

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143081

Add GROUP BY clause enumerating whatever you intend to have separate rows for. What happens now is that it picks some value for each result column and group_concats every pr.price.

Upvotes: 1

Related Questions