简体   繁体   中英

How do I ignore results from JOIN table in MySQL query

I have the following query:

$img_sub_qry = "SELECT * FROM images 
JOIN imagsub ON images.image_id = imagsub.image_id
WHERE images.image_id IN ($thelist)";

The problem is that this query is populating a drop down box and I am getting double results. I only want the results from the images table but I need the JOIN in order to grab a column that only exists in the imagsub table. From FirePHP I can see the results from the images table coming through just fine but then it loops back and pulls the same results from imagsub and I don't need those results. I have tried "SELECT DISTINCT" to no avail. Can someone help me here. I am fairly new to PHP and this is the first time I have tried using IN.

I think you're on the right track with select distinct; just specify the columns you want. If you're populating a drop-down list, go like this:

$img_sub_qry = "SELECT DISTINCT [colname] as ddlvalue, [colname] as ddltext
    FROM images 
    JOIN imagsub ON images.image_id = imagsub.image_id
    WHERE images.image_id IN ($thelist)";

For the query you're trying, every row in the query is going to be distinct, probably, because of the ID columns.

You should explicitly specify what fields you are interested in.

SELECT i.* 
FROM images i
JOIN imagsub is ON i.image_id = is.image_id
WHERE i.image_id IN ($thelist)

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