Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I query the same table twice with this code (keeping GROUP_CONCAT)?
    primarykey
    data
    text
    <p>In the code below, which is doing largely what I want, I am querying an attributes table that is linked to a 'venues' table by a venue_attribute table giving a many-to-many.</p> <p>I am doing this to improve performance when displaying a large list over individual venue pages, where I use factory to load up venue objects. So far this new call is saving me a lot of time. </p> <p>Firstly I would like to know why I had to use DISTINCT with GROUP_CONCAT to avoid multiple values being shown per venue under a.name? I don't see where it joins twice? </p> <p>My second challenge is retrieving a rating value which is held in the attributes table, separately from the GROUP_CONCAT returned value. (I have put the table structure below the query). The rating returns within the GROUP_CONCAT (a.name) value but there is only one per venue and I need to show it in a separate column, such as a.rating, within the select statement. I should perhaps hold the rating value in the venue table, and will, if it is not easily retrievable - but I'm interested to see what MYSQL can do and I'm rather basic in my skill levels. </p> <p>Thanks for your time. </p> <pre><code>SELECT v.venue_id, v.name, v.suburb, v.venue_email, v.venue_phone, GROUP_CONCAT(DISTINCT a.name) AS attributes, v.review_count, vi.image_thumb_path FROM venues AS v LEFT JOIN venue_attribute AS va ON v.venue_id = va.venue_id LEFT JOIN attribute AS a ON a.att_id = va.attribute_id LEFT JOIN venue_review AS vr ON vr.venue_id = v.venue_id LEFT JOIN venue_image AS vi ON vi.venue_id = v.venue_id WHERE v.status = 1 GROUP BY v.name; Table attribute =============== att_id, type, name, abbreviation, synonyms, description, scope --------------- att_id int(11) PK type enum('transport','purpose','parking','site_control','authorisations','permissions','facilities','rating') name varchar(255) abbreviation varchar(32) synonyms varchar(255) description mediumtext scope enum('global') </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload