Need Some Help With Sql Command

Discussion in 'Databases' started by joliett89, Feb 16, 2013.

  1. #1
    I have a MySQL database, with a table called "posts", and two fields of my interest are "userid" and "source". I am trying to create an SQL command, which would insert a text string "http://domain-name.com" to all source fields that are empty, and that belong to user id, let's say 55.

    So I need something like this:

    Insert text string "domain-name.com" to table named "posts", into field called "source" when "userid" is "55"

    (dont insert noting if the url, or something else is already there)

    There is going to be more empty fields as the time goes by, and I am going to be adding the same url on a regular basic. So most of the fields will be already populated, but there are going to be new ones, and I am gonna have to enter the url there (let's say every week or two etc; only to the empty ones, I gues, or whichever way it is suppose to work)

    Can somebody help me with that?

    Thank you.
     
    joliett89, Feb 16, 2013 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    insert into posts (source) values('domain.com') where userid=55 and (source = '' or source is null);
     
    Rukbat, Feb 16, 2013 IP
  3. worldart

    worldart Banned

    Messages:
    26
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    28
    #3
    First of all Insert into wont have conditions it will insert new records.

    If you have empty fields so i am assuming that you already have records in it with field userid where you want condition 55 so you can simpy fire update command

    update posts set source="domainmane" where userid=55

    here i am assuming that userid is numeric otherwise it will be in quotes
     
    worldart, Feb 19, 2013 IP
    ritesh8385 likes this.
  4. Athiyamaan B

    Athiyamaan B Greenhorn

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #4
    use TRIGGER option, you will find better results.

    By using trigger in MySQL database ll insert values in source automatically when it encounters the userid value
     
    Athiyamaan B, Mar 2, 2013 IP
  5. Hashim Lababdeh

    Hashim Lababdeh Greenhorn

    Messages:
    12
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    23
    #5
    u need programming for insert the table from any language programing .

    php or asp
     
    Hashim Lababdeh, Mar 11, 2013 IP