如何在MySQL查询中忽略连接表的结果

时间:2021-09-14 00:23:13

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.

问题是这个查询填充了一个下拉框,我得到了两个结果。我只需要来自图像表的结果,但我需要连接来获取只存在于imagsub表中的列。从FirePHP中,我可以看到来自图像表的结果很好,但是它会循环回来,并从imagsub中提取相同的结果,我不需要这些结果。我试过“选择不同的”,但没有效果。有人能帮我一下吗?我对PHP非常熟悉,这是我第一次尝试使用它。

2 个解决方案

#1


3  

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.

对于正在尝试的查询,由于ID列,查询中的每一行可能都是不同的。

#2


0  

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)

#1


3  

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.

对于正在尝试的查询,由于ID列,查询中的每一行可能都是不同的。

#2


0  

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)