Jump to content

Better type of query to use


spinner0205

Recommended Posts

I have the following MySQL query;

mysql_query("UPDATE thatsact_tag_clan.tag_user
					SET membergroupids='11'
					WHERE username='$vbid';");

 

And what I would like to do is change this to a better query that adds the number 11 to the existing list. At the moment it will take any numbers in this table field and overwrite them with 11. What I want it to do is add the number to the list. It is formatted as 1,2,3 and so on, so just have it add 11 to this list. What is a query I can use for this?

 

Thanks in advance.

Link to comment
Share on other sites

What 'list' are talking bout Willis? :shrug:

 

Mysql has databases that have tables that have rows and columns. Are talking about a column? If so your query is almost right - the problem is the table name

thatsact_tag_clan.tag_user  You can't use a dot in a table name.

 

Well maybe you can in ver 5 BUT just don't do it. 

 

Your query is UPDATING. You want to ADD. what do you want to add? By the way that's called INSERT, but we need to know what your going to add. Please tell us. And maybe name the columns in the table your adding to.

Link to comment
Share on other sites

I guess I call it a list because its a list of numbers in the field that goes by 1,2,3,4,5 etc. And what I want the query to do is add the number 11 to this set of numbers with the comma and everything. Also the query works fine with the period in version 5, already tested it.

 

EDIT: Probably confused you because I pasted the entire code including PHP. Edited for just the MySQL part below.

 

UPDATE thatsact_tag_clan.tag_user

SET membergroupids='11'

WHERE username='$vbid'

 

thatsact_tag_clan is the database and tag_user is the table. The field is membergroupids.

Link to comment
Share on other sites

For your existing scheme of storing a comma separated list in a field, you would use the mysql CONCAT() function in your query to concatenate the new value onto the end of the existing list.

 

SET membergroupids=CONCAT(membergroupids, ',11')

 

However, storing a comma separated list in a field is not efficient for finding information in and you should store this information using a table with a separate row for each value for each id that it belongs to.

 

user_id group_id
1          1
1          2
1          3
1          11

22       4
22      11

91      5
91      6

 

Link to comment
Share on other sites

Let me guess, you replaced your whole query statement - UPDATE thatsact_tag_clan.tag_user SET membergroupids='11' WHERE username='$vbid'; with the short fragment I posted showing how you would use CONCAT to set the membergroupids value?

Link to comment
Share on other sites

Yea I was at work doing multiple things so wasn't paying attention but now I added the whole thing and works great. However I have a new problem. How can I have it insert just the 11 rather than ,11 if there is no data in the membergroupids field yet?

 

If you cant find anything I could do I can just put a 0 in the field for everyone as the default.

Link to comment
Share on other sites

Assuming that the default for the column could be either a NULL or an empty string -

UPDATE thatsact_tag_clan.tag_user SET membergroupids=CONCAT(IFNULL(membergroupids,''),IF(LENGTH(IFNULL(membergroupids,''))>0,',',''),'11') WHERE username='$vbid'

Link to comment
Share on other sites

Well you are certainly good at, thanks a lot works wonders. Also not important but if possible some code for when the 11 or ,11 is already there then don't insert it? Sorry but MySQL is my down fall and that is why I come to this great site.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.