【问题标题】:Update table in users_meta in MySQL(Not wordpress)更新 MySQL 中 users_meta 中的表(不是 wordpress)
【发布时间】:2014-01-16 19:09:30
【问题描述】:

我想对持有“用户元信息”的表执行更新查询。

我要更新的meta_value对应“userid”=9,其中可以有多个meta_key进行更新。 (这不是wordpress数据库)

我的桌子是这样的:

umeta_id     userid      meta_key       meta_value
 1              9         mobile        123324
 2              9         address       some address
 3              9         city          some city
 4              9         country       some country
 5              9         occupation    some details
 6              9         website       someurl
 7              9         mobile        123324
 8              9         address       some address
 9              9         city          some city
 10             10        country       some country
 11             10        occupation    some details
 12             10        website       someurl

请为此提出一些解决方案。

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    更新 1:

    如果我想更新多个值,它可以是任何 meta_key,它现在不存在,但可以存在。我正在使用 PHP 来更新这个我的函数看起来像这样

    公共静态函数更新 ( $fullname, $mobile, $interest, $occupation, $address, $city, $country, $about, $‌​网站){ $sql = "更新用户元集"; };

    您可以使用CASE WHEN 或IF( ... ) 条件来更新列的多行。

    以下示例应该对您有所帮助。

    使用IF(...)函数:

    update user_meta_info
      set
         meta_value = if( meta_key='mobile', '$mobile', meta_value ),
         meta_value = if( meta_key='address', '$address', meta_value ),
         meta_value = if( meta_key='city', '$city', meta_value ),
         meta_value = if( meta_key='country', '$country', meta_value ),
         meta_value = if( meta_key='occupation', '$occupation', meta_value ),
         meta_value = if( meta_key='website', '$website', meta_value )
    where
      user_id = 9
    

    使用CASE ... WHEN ...:

    update user_meta_info
      set
         meta_value = case meta_key
                            when 'mobile' then '$mobile'
                            when 'address' then '$address'
                            when 'city' then '$city'
                            when 'country' then '$country'
                            when 'occupation' then '$occupation'
                            when 'website' then '$website'
                            else meta_value
                      end
    where
      user_id = 9
    

    您必须在 where 子句中使用 meta_key 为 meta_value 列设置新值。

    update user_meta_info
     set meta_value='new mobile number'
    where
     user_id = 9 and
     meta_key = 'mobile'
    

    如果每个meta_key 可以有多个元值,那么你必须在where 子句中使用umeta_id。如果umeta_id 是主键,则可以在where 子句中省略使用其他字段。

    update user_meta_info
     set meta_value='new mobile number'
    where
     umeta_id = 7 -- ( and user_id = 9 and meta_key = 'mobile' )
    

    【讨论】:

    • 如果我想更新多个值,它可以是任何 meta_key,它现在不存在,但可以存在。我正在使用 PHP 来更新这个我的函数看起来像这个公共静态函数 update($fullname,$mobile,$interest,$occupation,$address,$city,$country,$about,$website){ $sql = "update user_meta SET "; };
    • 请检查这个链接,是可能的。 stackoverflow.com/questions/21157511/…。有人已经回答但正在寻找更动态的方式。其中我不受 meta_key 限制,这意味着可以从行将任意数量的元密钥生成到 coloum 中。谢谢你!再一次。
    • @user3201500:是的。该解决方案正在旋转数据。在这个update ... 中,它是以类似的方式完成的。
    猜你喜欢
    • 2012-12-05
    • 2016-11-06
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多