Jump to content

[SOLVED] DOUBLE WHERE


jaymc

Recommended Posts

I have this query

 

UPDATE topic_views

SET views = views + 1, timestamp = '$time'

WHERE topic = '$topic' AND username = '$User'

LIMIT 1

 

That works fine, however there is another field I need to add, and I only want it to update that field if a condition is met. the other SET stuff should always complete. Basically something like this, but correct syntax

 

UPDATE topic_views

SET views = views + 1, timestamp = '$time', (last_id = '$latestID' WHERE last_id < 'oldID')

WHERE topic = '$topic' AND username = '$User'

LIMIT 1

 

Here is the part extracted

(last_id = '$latestID' WHERE last_id < 'oldID')

 

If that condition is met, it updates the last_id field and does the UPDATE on views and timestamp

If the condiotion is not met, it just updates views and timestamp, NOT last_id

 

Can this be done in one query? If so, how

Link to comment
Share on other sites

Sure, you can use an IF().

Can you post an example

 

I have search through google but not getting many examples as its picking up IF as a word in peoples posts on other forums rather than IF as MYSQL

Link to comment
Share on other sites

Although Im not able to add update multiple fields in the IF statement

 

last_post_read = IF((last_post_read < '50', '4', last_post_read) AND '2',last_post_position)

 

That wont work, at a guess

Link to comment
Share on other sites

If they view a topic, it will always update the last view time

 

Also in that table is a field which shows last post read

 

I needed to check if the last post read was lower than the current post reading

 

I got it working in the end but I had to use two IF statements in the query as a fudge, I guess thats not the correct way

 

last_post_read = IF((last_post_read < '50', '4', last_post_read),

last_post_id = IF(last_post_read < '50', '4', last_post_id)

 

Ignore the values 50 and 4, in reality they are php vars

 

But yeh, thats how I did it, how can I turn that into a single if and add a multi field update inside the IF

 

 

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.