MySQL查询联结表会返回重复的行

时间:2017-08-17 09:16:56

标签: php mysql join junction-table

我有以下表格结构: enter image description here

因此,产品可能有多个kedar@myhost:/opt$ sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/jdk1.8.0_72/bin/javac" javac kedar@myhost:/opt$ sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/jdk1.8.0_72/bin/javac" 1 kedar@myhost:/opt$ ll javac ls: cannot access javac: No such file or directory kedar@myhost:/opt$ sudo update-alternatives --config java There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java Nothing to configure. kedar@myhost:/opt$ sudo update-alternatives --config javac There are 2 choices for the alternative javac (providing /usr/bin/javac). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-7-openjdk-amd64/bin/javac 1071 auto mode 1 /opt/jdk1.8.0_72/bin/javac 1 manual mode 2 /usr/lib/jvm/java-7-openjdk-amd64/bin/javac 1071 manual mode Press enter to keep the current choice[*], or type selection number: 1 update-alternatives: using /opt/jdk1.8.0_72/bin/javac to provide /usr/bin/javac (javac) in manual mode kedar@myhost:/opt$ sudo update-alternatives --config javac There are 2 choices for the alternative javac (providing /usr/bin/javac). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-7-openjdk-amd64/bin/javac 1071 auto mode * 1 /opt/jdk1.8.0_72/bin/javac 1 manual mode 2 /usr/lib/jvm/java-7-openjdk-amd64/bin/javac 1071 manual mode Press enter to keep the current choice[*], or type selection number: kedar@myhost:/opt$ kedar@myhost:/opt$ kedar@myhost:/opt$ kedar@myhost:/opt$ sudo update-alternatives --install "/usr/bin/java" "java" "/opt/jdk1.8.0_72/bin/java" 1 kedar@myhost:/opt$ ll javac ls: cannot access javac: No such file or directory kedar@myhost:/opt$ sudo update-alternatives --config java There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1071 auto mode 1 /opt/jdk1.8.0_72/bin/java 1 manual mode 2 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1071 manual mode Press enter to keep the current choice[*], or type selection number: 1 update-alternatives: using /opt/jdk1.8.0_72/bin/java to provide /usr/bin/java (java) in manual mode kedar@myhost:/opt$ cd kedar@myhost:~$ cd $SPARK_HOME kedar@myhost:~/software/spark$ ./sbin/stop-all.sh localhost: no org.apache.spark.deploy.worker.Worker to stop stopping org.apache.spark.deploy.master.Master kedar@myhost:~/software/spark$ ./sbin/start-all.sh starting org.apache.spark.deploy.master.Master, logging to /home/kedar/software/spark/logs/spark-kedar-org.apache.spark.deploy.master.Master-1-myhost.out localhost: starting org.apache.spark.deploy.worker.Worker, logging to /home/kedar/software/spark/logs/spark-kedar-org.apache.spark.deploy.worker.Worker-1-myhost.out

当我加入MySQL时,我会为每个product_type重复product条记录。例如,如果我有名为product_type的产品并且它在Actifoam表中有2条记录,那么查询结果将包括products_types两次(每个都有不同的Actifoam }。

这是我的问题:

product_type

以下是查询结果显示的内容:

SELECT DISTINCT product.*, product_type.name as product_type_name
FROM products_types
JOIN product_type ON product_type.id = products_types.product_type_id
JOIN product ON product.id = products_types.product_id

我不希望[ 0 => [ 'id' => 'recccAQHxsb4OEsX6' 'name' => 'Actifoam' 'product_type_name' => 'Silver Dressing' ] 1 => [ 'id' => 'recccAQHxsb4OEsX6' 'name' => 'Actifoam' 'product_type_name' => 'Foam' ] ] 记录多次显示,如果有多个product,我希望所有内容都显示在一行中。

任何人都知道如何实现这一目标?

1 个答案:

答案 0 :(得分:2)

仅当{2}在DISTINCT语句中的所有列具有完全相同的相应值时,

SELECT才会返回唯一行。在您的情况下,您需要GROUP BYGROUP_CONCAT

GROUP_CONCAT将以逗号分隔product_type_name

SELECT product.*, GROUP_CONCAT(product_type.name) as product_type_name
FROM products_types
JOIN product_type ON product_type.id = products_types.product_type_id
JOIN product ON product.id = products_types.product_id
GROUP BY product.id