MySQL 'multiple rows' from two joins challenge

Recardo Patrese

I have the following MySQL query joining two additional tables..

SELECT  plants.plant_id,
        plants.scientific_name,
        plants_features.features,
        plants_features.features_id

FROM    plants_features,
        plants2features,
        plants

WHERE   plants_features.features_id = plants2features.features_id

AND     plants2features.plant_id = plants.plant_id

and I was happy with the output.

plant_id,scientific_name,features,features_id
3,"Actaea matsumurae 'White Pearl' (Bugbane)","colourful fruit",6
11,"Heuchera 'Beauty Colour' (Coral bells)","salt resistant/coastal",15
18,"Phyllostachys nigra (Black bamboo)","colourful bark",5
26,"Carex morrowii 'Silver Sceptre' (Japanese sedge)","drought tolerant",18
27,"Heuchera 'Obsidian' (Coral bells)","salt resistant/coastal",15
29,"Dianella tasmanica 'Tas Red' (Flax lily)","drought tolerant",18
38,"Stipa tenuissima (Mexican feather grass)","attractive seed-heads",2
38,"Stipa tenuissima (Mexican feather grass)","invasive/self seed/suckering",13

You can see from the plant_id '38' (bottom two rows) that it's outputting more than one row per record.

My question is guys, can you please advise what exactly the new MySQL query needs to be to ensure multiple rows are in one record by 'plant_id'?

Thanks in advance, Richard.

Lukasz Szozda

You need grouping and aggregation functions. You can use GROUP_CONCAT to get comma separated list:

SELECT  plants.plant_id,
        plants.scientific_name,
        GROUP_CONCAT(plants_features.features) AS features,
        GROUP_CONCAT(plants_features.features_id) AS feature_ids
FROM plants_features
JOIN plants2features
  ON plants_features.features_id = plants2features.features_id
JOIN plants
  ON plants2features.plant_id = plants.plant_id
GROUP BY plants.plant_id, plants.scientific_name;

Also use JOIN syntax instead of comma and where.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to select one row from one table and multiple rows from other table using joins in mysql,

From Dev

MySQL multiple joins with multiple conditions and ignore row based on other rows

From Dev

MySQL - Display multiple rows in one field (tables with inner joins)q

From Dev

Getting data from multiple rows using joins in phpMyAdmin

From Dev

mysql - multiple joins

From Dev

mySQL multiple inner joins

From Dev

Multiple joins in MySQL table

From Dev

Optimize multiple JOINs in MySQL

From Dev

Multiple JOINS - Mysql

From Dev

Multiple Inner Joins - MySQL

From Dev

mySQL multiple inner joins

From Dev

MySQL joins multiple tables

From Dev

PHP pull from two MySQL tables, where multiple rows in table two

From Dev

Select multiple rows from MySQL

From Dev

MySQL Selecting from multiple rows

From Dev

SQL Duplicate Rows Multiple Joins

From Dev

Multiple Master Challenge | MySQL & MariaDB

From Dev

Multiple Master Challenge | MySQL & MariaDB

From Dev

Retrieve records from two tables using MySQL JOINS

From Dev

MySQL JOIN two tables and return multiple rows

From Dev

Tips to speed up multiple joins from a MySQL database

From Dev

Combine multiple rows from multiple tables in MySQL

From Dev

How to do LEFT JOIN two tables and exclude multiple rows from main query in subquery using MySQL?

From Dev

Combine multiple rows into one row MySQL and split value from one field to two in same Query

From Dev

mysql search with two joins and a count

From Dev

Select last two rows from mysql php

From Dev

PHP and MySql - fetching rows from two tables

From Dev

MySQL multiple joins and count distinct

From Dev

Multiple mysql table joins with php?

Related Related

HotTag

Archive