简体   繁体   English

我将如何消除包含concat的更新查询中的重复项

[英]How would I go about eliminating duplicates in a update query that includes a concat

Here is the query I have to call each time a new item is added to the database : 这是每次将新项目添加到数据库时我都必须调用的查询:

UPDATE `cas_phocagallery`,`cas_phocagallery_categories`
   SET cas_phocagallery.description=concat(
       cas_phocagallery_categories.title,'<br />',cas_phocagallery.description)
 WHERE cas_phocagallery.catid = cas_phocagallery_categories.id;

It works fine except that it produces a duplicate each time I run the query. 它工作正常,只是每次运行查询时都会产生重复。 I tried to add distinct but I get an error and I am not sure this is the right thing to do in this case. 我尝试添加distinct,但出现错误,并且不确定在这种情况下这样做是否正确。

Thank you for your cooperation, I have been looking at all kinds of ways all day with no success. 谢谢您的合作,我一直在寻找各种方法,但没有成功。

When you have a unique key, it should produce an error when inserting a duplicate value and is expected behaviour. 如果您拥有唯一键,则在插入重复值时应该会产生错误,并且这是预期的行为。 I think what you should be looking at is on duplicate key update where you can specify what to do if a duplicate is found. 我认为您应该关注的是重复密钥更新 ,您可以在其中指定如果发现重复密钥该怎么办。

Every time you run the query, you add the title to the current description . 每次运行查询时,都将title添加到当前description If you run it three times in a row, you will get 如果连续运行三次,您将获得

title    <-- added by 3rd
title    <-- added by 2nd
title    <-- added by 1st
description (original)

Is that what you mean? 你是这个意思吗? That is exactly what the query is designed to do. 这正是查询旨在执行的操作。 Maybe you need another column to keep the "base_description", so that each time, "description" gets to be "title" + br + "base_description" 也许您需要另外一列来保留“ base_description”,以便每次“ description”成为“ title” + br +“ base_description”


EDIT 编辑

You can try something like this, which would work 95% of the time, until the title changes in which case you will get the historical titles built up. 您可以尝试这样的方法,这种方法将在95%的时间内起作用,直到标题更改为止,在这种情况下,您将获得历史标题。 It also doesn't work when the title contains the % symbol and may work funny if it contains the _ character. 当标题包含%符号时,它也不起作用;如果包含_字符,则可能很有趣。

UPDATE cas_phocagallery, cas_phocagallery_categories
   SET cas_phocagallery.description=concat(
       cas_phocagallery_categories.title,'<br />',cas_phocagallery.description)
 WHERE cas_phocagallery.catid = cas_phocagallery_categories.id
   AND NOT cas_phocagallery.description
           LIKE concat(cas_phocagallery_categories.title, '%')

Can't really comment on Joomla or how it does things or why this would be necessary (to merge title into description). 不能真正评论Joomla或它的工作方式或为什么要这样做(将标题合并为描述)。 What you should really do is in some SELECT statement in some module, show the result of the CONCAT instead of persisting (pushing/mangling) it into description. 您真正应该做的是在某个模块的某些SELECT语句中,显示CONCAT的结果,而不是将其持久化(推入/处理)到描述中。

   SELECT cas_phocagallery.*, concat(cas_phocagallery_categories.title,
                                     '<br />',
                                     cas_phocagallery.description) as FullDescription
     FROM cas_phocagallery
LEFT JOIN cas_phocagallery_categories
       ON cas_phocagallery.catid = cas_phocagallery_categories.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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