1

我正在构建一个分类广告的网络应用程序(这是我的第一个专业 PHP 应用程序)。我有一张带有广告的表格,还有一张带有每个广告图片的表格。每个广告最多可以有 4 张图片。当我每个广告只添加一张图片时,一切都很好。现在,当我为每个广告投放超过一张图片时,该广告会在列表中重复(每张图片一次)。这是我正在使用的 SQL:

SELECT 
sun_classified_ads.ad_id, 
sun_classified_ads.insertion_datetime, 
sun_classified_ads.title,                
sun_classified_ads.description, 
sun_classified_ads.maker, 
sun_classified_ads.model, 
sun_classified_ads.location, 
sun_classified_ads.price, 
sun_classified_ads_images.filename_mini, 
sun_classified_ads_images.size_attr_mini, 
sun_classified_ads_images.description AS image_description
FROM sun_classified_ads
INNER JOIN sun_classified_ads_images ON sun_classified_ads.ad_id =       sun_classified_ads_images.ad_id

我该怎么做才能只使用 sun_classified_ads_images 表中该广告的第一张图片?

4

3 回答 3

2

MySQL 有一个称为隐藏列的“功能”。您可以使用以下方法为每个广告获取一张图片:

SELECT 
sun_classified_ads.ad_id, 
sun_classified_ads.insertion_datetime, 
sun_classified_ads.title,                
sun_classified_ads.description, 
sun_classified_ads.maker, 
sun_classified_ads.model, 
sun_classified_ads.location, 
sun_classified_ads.price, 
sun_classified_ads_images.filename_mini, 
sun_classified_ads_images.size_attr_mini, 
sun_classified_ads_images.description AS image_description
FROM sun_classified_ads
INNER JOIN sun_classified_ads_images ON sun_classified_ads.ad_id =       sun_classified_ads_images.ad_id
group by sun_classified_ads.ad_id

只是一个警告。不能保证这将返回第一个广告。不能保证图像中的三个字段实际上来自同一记录(尽管实际上它们确实如此)。但是,这可能是获得所需内容的最简单方法。

以下查询确实可以满足您的要求,但需要做更多工作:

SELECT 
sun_classified_ads.ad_id, 
sun_classified_ads.insertion_datetime, 
sun_classified_ads.title,                
sun_classified_ads.description, 
sun_classified_ads.maker, 
sun_classified_ads.model, 
sun_classified_ads.location, 
sun_classified_ads.price, 
substring_index(group_concat(sun_classified_ads_images.filename_mini order by ad_image_id), ',', 1), 
substring_index(group_concat(sun_classified_ads_images.size_attr_mini order by ad_image_id), ',', 1), 
substring_index(group_concat(sun_classified_ads_images.description order by ad_image_id), ',', 1) AS image_description
FROM sun_classified_ads
INNER JOIN sun_classified_ads_images ON sun_classified_ads.ad_id =       sun_classified_ads_images.ad_id
group by sun_classified_ads.ad_id

这假定这ad_image_id是一个可用于定义图像顺序的字段。

于 2013-01-30T02:52:56.023 回答
1

您没有说明您的表格是否有datetime关于何时image添加的信息。如果是这样,那么您可以更改以下查询以返回min()每个广告的日期。

如果没有,那么您可能仍然可以使用以下内容:

SELECT 
  a.ad_id, 
  a.insertion_datetime, 
  a.title,                
  a.description, 
  a.maker, 
  a.model, 
  a.location, 
  a.price, 
  i.filename_mini, 
  i.size_attr_mini, 
  i.description AS image_description
FROM sun_classified_ads a
INNER JOIN
(
  select min(ad_id) Min_Ad_id, ad_id
  from sun_classified_ads_images i
  group by ad_id
) i
  ON a.ad_id = i.ad_id
INNER JOIN sun_classified_ads_images i2
  ON a.ad_id = i2.ad_id
  and i.Min_Ad_id = i2.ad_id

如果您datetime在 images 表中有一个,那么您可以使用:

INNER JOIN
(
  select min(ad_date) Min_Ad_Date, ad_id
  from sun_classified_ads_images i
  group by ad_id
) i
  ON a.ad_id = i.ad_id
INNER JOIN sun_classified_ads_images i2
  ON a.ad_id = i2.ad_id
  and i.Min_Ad_Date = i2.ad_date
于 2013-01-30T02:51:25.813 回答
0

简短的回答是“你不能”。您编写的查询将返回所有匹配数据的所有行。每个图像的数据匹配一次。您需要执行两项查询——一项获取所有广告,然后一项在页面内查询所有图像。这是最直接的方法。

于 2013-01-30T02:48:30.380 回答