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
Related
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
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));
I want to fetch entire record with distinct field points so I wrote following query but it is not giving desired result.
#top_score_cards = Score.select("DISTINCT points *").order("points DESC , updated_at DESC").where("points != ? And activity_id= ? ",0,params[:#myActivity]).limit(3)
It is throwing following error :
Mysql2::Error: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server versi on for the right syntax to
use near 'FROM scores WHERE (points != 0 And activity_id= '1' )
ORDER BY points DE SC ,' at line 1: SELECT DISTINCT points * FROM
scores WHERE (points != 0 And activity_id= '1' ) ORDER BY poi nts
DESC , updated_at DESC LIMIT 3
The logic of what you're asking for doesn't really make sense, since select will just return the points but you want the whole record.
I think i can guess what it's supposed to be though since i answered another related question earlier. :)
Am i right in thinking that you want one record for the 3 highest unique "points" value in the table, and when there is more than one record with the same points you want the one with the earliest updated_at? if so, then the key to this is group by, not select.
#top_score_cards = Score.group(:points).order("points DESC , updated_at").where("points <> ? and activity_id = ? ",0,params[:activity_id]).limit(3)
I've taken the liberty of fixing lots of syntax errors in your code, so i can't guarantee this this will work or is even what you want.
Then try this instead:
Score.where("points != ? And activity_id= ?",0,params[:#myActivity])
.order("points DESC, updated_at DESC")
.limit(3)
.uniq! { |x| x.points }
Based on the documentation: http://apidock.com/rails/ActiveRecord/QueryMethods/distinct
I put together a test table for a error I recently came across. It involves the use of LIMIT when attempting to delete a single record from a MySQL table.
The error I speak of is "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 1' at line 1"
The table I put together is called test; it has 3 columns, id, name and created. I populated the table with several records and then attempted to delete one. Below is the statement I used to try and accomplish this.
DELETE t FROM test t WHERE t.name = 'foo' LIMIT 1
Without the use of LIMIT 1, the statement executes just fine, but of course I wouldn't be using LIMIT if there wasn't a need for it.
I'm fully aware that I can use another statement to accomplish this DELETE successfully. See below:
DELETE FROM test WHERE name = 'foo' LIMIT 1
However my question is centered on why the first statement isn't working with LIMIT.
So my question is, what I have done incorrectly with respect to the first statement to generate this error?
simply use
DELETE FROM test WHERE 1= 1 LIMIT 10
the delete query only allows for modifiers after the DELETE 'command' to tell the database what/how do handle things.
see this page
From the documentation:
You cannot use ORDER BY or LIMIT in a multiple-table DELETE.
DELETE t.* FROM test t WHERE t.name = 'foo' LIMIT 1
#Andre If I understood what you are asking, I think the only thing missing is the t.* before FROM.
Use row_count - your_desired_offset
So if we had 10 rows and want to offset 3
10 - 3 = 7
Now the query delete from table where this = that order asc limit 7 keeps the last 3, and order desc to keep the first 3:
$row_count - $offset = $limit
Delete from table where entry = criteria order by ts asc limit $limit
There is a workaround to solve this problem by using a derived table.
DELETE t1 FROM test t1 JOIN (SELECT t.id FROM test LIMIT 1) t2 ON t1.id = t2.id
Because the LIMIT is inside the derived table the join will match only 1 row and thus the query will delete only this row.
First I struggled a bit with a
DELETE FROM ... USING ... WHERE query,...
Since i wanted to test first
so i tried with SELECT FROM ... USING... WHERE ...
and this caused an error , ...
Then i wanted to reduce the number of deletions adding
LIMIT 10
which also produced an error
Then i removed the "LIMIT" and - hurray - it worked:
"1867 rows deleted. (Query took 1.3025 seconds.)"
The query was:
DELETE FROM tableX
USING tableX , tableX as Dup
WHERE NOT tableX .id = Dup.id
AND tableX .id > Dup.id
AND tableX .email= Dup.email
AND tableX .mobil = Dup.mobil
This worked.
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.