如果存在多个默认值,请避免使用默认值,或者选择Mysql中的现有单个值
In my website, I have a product image display region. When a user click on a product, it will redirect to the image display page. I had set a default image. Which will display when user see the product. But when the user go to the product image page, I don't want to show the default image, if there exist more than one image. But I need to show the default image if there exist only one image in the database. Currently I am doing this with two queries. Is there any way to do this with a single query?
I need something like
select image from table where if exists more than 1 then select 'non-featured' else select 'featured'
Please help
在我的网站上,我有一个产品图片显示区域。 当用户点击产品时,它将重定向到图像显示页面。 我设置了默认图像。 当用户看到产品时会显示哪个。 但是当用户转到产品图像页面时,如果存在多个图像,我不想显示默认图像。 但是如果数据库中只有一个图像,我需要显示默认图像。 目前我正在做两个查询。 有没有办法用一个查询来做到这一点? p>
我需要类似 p>
从表中选择图像,如果存在多于 1然后选择'非特色',否则选择'精选'
code> pre>
请帮助 p>
div>
You can work yourself around by using the group function max()
. This function should always return you just 1 image, if it can find it. Otherwise it will return null
. Then use the function ifnull()
to get your default image.
select ifnull(max(image),'default_image.jpg') from table