简体   繁体   中英

Mysql inner join returning repeated results

Im trying to pull a character from a mysql database. 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. and pull the image url. The code i coded below to do this is giving me duplicate image urls. 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.

I made a sqlfiddle but it looks like its working correct here 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.

Example :

SELECT DISTINCT yourField FROM tables;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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