php/mysql help needed - if not exist, get proper value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spider22
    New Member
    • Feb 2015
    • 1

    php/mysql help needed - if not exist, get proper value

    I am trying to insert list of items from dev.test1 table, but I am struggling with logic ...

    Face following problems:

    - Before insert statements I would like to add "if not exists" - select content from dev.qa_postmeta s
    - f.URL which I am getting in second query is not coming from correct row in dev.test1 - If want to select f.URL where f.title = b.title

    Code:
    INSERT INTO dev.qa_posts (type, categoryid, userid, created, title, content, tags)
    (SELECT  'Q_QUEUED', '1', '3', NOW(), f.title, f.img, f.tagsv
    FROM dev.test1 f)
    LIMIT 1;
    
    INSERT INTO dev.qa_postmetas (postid, title, content) 
    (select MAX(b.postid) , 'qa_q_extra',f.URL
    from dev.qa_posts b
    left JOIN dev.test1 as f on b.postid = f.id)
    LIMIT 1 ;
    Any assistance will be appreciated
    Last edited by Rabbit; Mar 1 '15, 04:00 AM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    1) why is there a LIMIT clause in an INSERT statement?

    2)
    Before insert statements I would like to add "if not exists"
    make the table column that should not have duplicate entries UNIQUE. if you try to insert a duplicate value the DB will automatically reject the insert.

    3)
    If want to select f.URL where f.title = b.title
    then why don’t you write that into the ON clause?

    Comment

    Working...