简体   繁体   English

MySQL内部联接返回重复结果

[英]Mysql inner join returning repeated results

Im trying to pull a character from a mysql database. 我正在尝试从mysql数据库中提取字符。 The character table has 6 columns that link to a foreign item id from the item table, i need to link each item to get the item id, name, and foreign image id, then i need to link that foregin image id to my image table. 字符表有6列,可链接到项目表中的外部项目ID,我需要链接每个项目以获取项目ID,名称和外部图像ID,然后我需要将该前例图像ID链接到我的图像表。 and pull the image url. 并提取图片网址。 The code i coded below to do this is giving me duplicate image urls. 我在下面编码的代码做到了这一点,这给了我重复的图像URL。 Does anyone know whats wrong with it? 有谁知道这是怎么回事?

This is my results. 这是我的结果。 The image urls are repeating themselfs. 图片网址在重复自己。 I took project_users out to test it and the same thing happened its not being used now but will be used in the future. 我带了project_users进行测试,同样的事情发生了,现在不使用它,但是将来会使用。

I made a sqlfiddle but it looks like its working correct here http://sqlfiddle.com/#!2/7896e/8 我做了一个sqlfiddle,但是看起来它在这里工作正常http://sqlfiddle.com/#!2/7896e/8

    Array ( [name] => test1241 [0] => test1241 [gender] => [1] => [left_arm] => Images/Items/leftArm.png [2] =>
 Images/Items/leftArm.png [legs] => Images/Items/legs.png [3] => Images/Items/legs.png [torso] => 
Images/Items/torso.png [4] => Images/Items/torso.png [head] => Images/Items/head.png [5] => Images/Items/head.png [hair] => Images/Items/hair.png [6] => Images/Items/hair.png [right_arm] => Images/Items/rightArm.png [7] => Images/Items/rightArm.png )

   $sql = "SELECT
            pc.project_characters_name as name,
            pc.project_characters_gender as gender,
            pia1.project_images_url as left_arm,
            pia2.project_images_url as legs,
            pia3.project_images_url as torso,
            pia4.project_images_url as head,
            pia5.project_images_url as hair,
            pia6.project_images_url as right_arm

            FROM project_characters AS pc
            INNER JOIN project_users AS pu ON pc.fk_project_users_id = pu.project_users_id
            INNER JOIN project_items AS pi1 ON pc.project_characters_left_arm = pi1.project_items_id
            INNER JOIN project_items AS pi2 ON pc.project_characters_legs = pi2.project_items_id
            INNER JOIN project_items AS pi3 ON pc.project_characters_torso = pi3.project_items_id
            INNER JOIN project_items AS pi4 ON pc.project_characters_head = pi4.project_items_id
            INNER JOIN project_items AS pi5 ON pc.project_characters_hair = pi5.project_items_id
            INNER JOIN project_items AS pi6 ON pc.project_characters_right_arm = pi6.project_items_id
            INNER JOIN project_images AS pia1 ON pi1.fk_project_images_id = pia1.project_images_id
            INNER JOIN project_images AS pia2 ON pi2.fk_project_images_id = pia2.project_images_id
            INNER JOIN project_images AS pia3 ON pi3.fk_project_images_id = pia3.project_images_id
            INNER JOIN project_images AS pia4 ON pi4.fk_project_images_id = pia4.project_images_id
            INNER JOIN project_images AS pia5 ON pi5.fk_project_images_id = pia5.project_images_id
            INNER JOIN project_images AS pia6 ON pi6.fk_project_images_id = pia6.project_images_id
            WHERE pc.project_characters_name=:name LIMIT 1";

You can try using the keyword DISTINCT to select only distinct result. 您可以尝试使用关键字DISTINCT仅选择不同的结果。

Example : 范例:

SELECT DISTINCT yourField FROM tables;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM