this is how i select highest id for a specific username
SELECT * FROM messages WHERE user='me' ORDER BY id DESC LIMIT 0, 1
i have column named "send" and a user named me
now i want to update it like this :
UPDATE messages SET `send`='ok' WHERE user='me' ORDER BY id DESC LIMIT 0, 1
i get syntax error
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 1' at line 1
The limit in update only allows a row count, not an offset:
UPDATE messages
SET `send`='ok'
WHERE user='me'
ORDER BY id DESC
LIMIT 1;
I feel like you're setting yourself up for failure here.
If you're going to select the highest ID before updating, then:
SELECT max(id) FROM messages WHERE user='me'
Your UPDATE should be using that same ID you just retrieved as the WHERE clause. Otherwise, you can potentially update the wrong row with "ok". Full code, as I would do it:
// Get ID to update
$idToUpdate_q = $pdoConn->prepare("SELECT max(messages.id) FROM messages WHERE user = :user");
$idToUpdate_q->bindValue(':user','me');
$idToUpdate_q->execute();
$idToUpdate = $idToUpdate_q->fetchColumn();
// Update Row
$updateRow_sql = $pdoConn->prepare("UPDATE messages
SET
messages.send = :send
WHERE
messages.id = :idToUpdate");
$updateRow_sql->execute(array(
':send' =>'ok',
':idToUpdate' =>$idToUpdate));
Related
I have a website with a members section where users can include an affiliate link in their profile. The home page of the website should show one affiliate link at a time, rotating through users which have set an affiliate link. My server is running php5.6.
I've tried using rand() with IS NOT NULL to select users from the website's SQL database in a few ways:
SELECT * FROM users IS NOT NULL ORDER by rand() LIMIT 1;
but I get an error. I also have tried
SELECT leads ORDER by rand() FROM users IS NOT NULL LIMIT 1;
Still no results.
SELECT * FROM users IS NOT NULL LIMIT 1;
It works but it will not randomly select through the null data.
I keep getting the following errors:
ERROR: Could not able to execute SELECT * FROM users IS NOT NULL ORDER
by rand() LIMIT 1. You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'IS NOT NULL ORDER by rand() LIMIT 1' at line 1
and below this does not give an error but it does not do what I need it to do.
SELECT leads IS NOT NULL FROM users ORDER by rand() LIMIT 1;
You have issue with checking NOT NULL. You have to check NULL in WHERE clause and also by specific column name. You can add 1 or Multiple or even All columns in the WHERE clause as per your requirement. Please try as below-
SELECT * FROM users
WHERE column_name1 IS NOT NULL
AND column_name2 IS NOT NULL
-- You can add here more columns
-- using AND or OR depending on what you wants to check.
ORDER BY RAND() LIMIT 1
Trying to get a list of the latest forum posts from a user, ordered by the date it was made.
The problem is posts are split between topics and replies, so i need to get the reply text from a different table if the latest post isn't the topic itself.
Trying to use CASE to switch between a subquery if it's a reply else use the topic text.
SELECT
t.`topic_id`,
t.`topic_title`,
t.`last_post_date`,
t.`last_post_id`,
CASE WHEN t.`replys` > 0 THEN (
SELECT
`reply_text` AS 'text'
FROM
`forum_replies`
WHERE
`post_id` = t.`last_post_id`
) ELSE t.`topic_text` AS 'text'
END
FROM
`forum_topics` t
WHERE
t.`approved` = 1 AND t.`forum_id` IN (1) AND t.last_post_user_id = 1
ORDER BY
t.`last_post_date`
DESC
LIMIT 5
The error is:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'AS 'text' END FROM
forum_topics t WHERE
t.approved = 1 AND t.`f' at line 13
Use of subquery in CASE is not allowed. Use LEFT JOIN instead:
SELECT
t.`topic_id`,
t.`topic_title`,
t.`last_post_date`,
t.`last_post_id`,
t.`reply_text` as 'text'
FROM
`forum_topics` t
LEFT JOIN `forum_replies` r on r.`post_id` = t.`last_post_id`
WHERE
t.`approved` = 1 AND t.`forum_id` IN (1) AND t.last_post_user_id = 1
ORDER BY
t.`last_post_date`
DESC
LIMIT 5
I have an SQL field defined as set('nightlife', 'food', 'sports', 'culture', 'movies', 'general')
Now I want to run a query where I pass for example nightlife,food and I want the result to contain ALL records where the Category contains nightlife or food. So for example a record with nightlife,culture,sports should also be returned as it contains nightlife. I'm not sure how to run this. I tried using the IN keyword in the following way:
'SELECT ... FROM table WHERE '$category' IN Categories
however this isn't working.
UPDATE
Running following query as suggested in answer:
SELECT *
FROM images
WHERE id = '2650225'
AND WHERE FIND_IN_SET('sports', Categories ) >0
OR FIND_IN_SET('nightlife', Categories ) >0
ORDER BY delete_at ASC
LIMIT 10
OFFSET 0
receiving following error :
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE find_in_set('sports', Categories) > 0 or find_in_set('nightlife', Categori' at line 1
You can use find_in_set with or:
select *
from yourtable
where find_in_set('nightlife', categories) > 0 or
find_in_set('food', categories) > 0
SQL Fiddle Demo
Based on your edits, you can't have multiple where clauses. Also you need to use parentheses around the or criteria:
SELECT *
FROM images
WHERE id = '2650225' AND
(FIND_IN_SET('sports', Categories ) > 0 OR
FIND_IN_SET('nightlife', Categories ) >0)
ORDER BY delete_at ASC
LIMIT 10
OFFSET 0
I have a telephone number which i want to use to get the next record id using limit value,1 method.
This is my sql code
select id as next_id
from transactions
where tel = '44723954537'
AND id = (select id from transactions where tel = '44723954537')
LIMIT (select id from transactions where tel = '44723954537'),1
When i run the code,i get this error
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'LIMIT (select id from transactions where tel = '44723954537'),1)
LIMIT 0, 30' at line 1
How should i write my limit statement?.
SELECT min(id) AS next_id
FROM transactions
WHERE id > (SELECT MAX(id)
FROM transactions
WHERE tel = '4472395437')
This will find the next record after the last record with the given telephone number.
UPDATE table SET checked = 1 WHERE field = 'xxx' LIMIT 1
works fine, but
UPDATE table SET checked = 1 WHERE field = 'xxx' LIMIT 1, 10
throw error "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 10' at line 1"
Why it is not possible? I want to update everything except first row.
update table set checked = 1 where id in (select * from (select id from table where field = 'xxx' order by id limit 1, 10) as t)
LIMIT in an UPDATE clause is merely an upper limit on how many rows may be updated.
It's not like in a SELECT where you can ignore all but a certain subrange of result rows to deal with.
If you really need something like this, you should use a VIEW with the LIMIT restriction, and perform the UPDATE on that.
I had a similar situation, but in my case I needed to update only 2 rows ordered by a numerical ID, so my query would've been like this:
UPDATE myTable
SET Column1='some data',Column2='some othe data'
WHERE Column3='some criteria' LIMIT 1;
UPDATE myTable
SET Column1='some data',Col2='some othe data'
WHERE Column3='some criteria2' ORDER BY ID DESC LIMIT 1;
Note: The first query implicitly selects the first matching row in the table, and the second query selects the second matching row by explicitly reversing the order. It doesn't answer the question but it may benefit someone with problem similar to mine.