I have been trying to debug this for quite a while now with no success.
It seems that my mySQL query breaks as soon as I add a where clause to the below query.
Note this returns no results when it should:
SELECT * FROM `workorders` WHERE (`status`='Open') AND
`job_site_name` LIKE '%".$searchword."%' ORDER BY `date_promised` ASC LIMIT 20
This is the same query without the where clause and it returns results as expected, but I need the where status=Open as part of the query...
SELECT * FROM `workorders` WHERE
`job_site_name` LIKE '%".$searchword."%' ORDER BY `date_promised` ASC LIMIT 20
Furthermore, this works (and shows that the status column exists and works in other cases):
SELECT * FROM `workorders` WHERE (`status`='Open') AND `invoice_number` LIKE
'%".$searchword."%' ORDER BY `date_promised` ASC LIMIT 20
I appreciate any assistance in identifying the cause of this problem.
Many thanks in advance!
The issue seems to be both criteria are not met. If you need to use an OR clause then you can do
SELECT *
FROM `workorders`
WHERE (`status`='Open'
OR `job_site_name` LIKE '%".$searchword."%')
ORDER BY `date_promised` ASC
LIMIT 20
Related
I have data in MYSQL table where i have to paginate through 4 rows at a time.
To do this I use the command below where i increase X from 0 by 4 until i reach the end of the data.
The command works for X=0 and X=4, when i reach X=8 i get the error #1038 - out of sort memory, i tried increasing it to 256K but same result.
Anybody know how to solve? Im using PHP
SELECT DISTINCT * FROM ((SELECT DISTINCT * FROM table WHERE (scope=0) AND (id='6')) UNION (SELECT DISTINCT * FROM table WHERE (scope=0) AND (id<=1000))) as total ORDER BY timestamp DESC LIMIT X,4
Not an answer, but your query seems to be functionally identical to this:
SELECT columns
, you
, actually
, want
FROM table
WHERE scope = 0
AND id<=1000
ORDER
BY timestamp DESC
LIMIT X,4
I'm trying to make a quiz using mysql and php
I want to fetch questions randomly from the database
I have a truble with rand function
sometimes it seturns no value and returns also duplications
I tried to find a solution on the net but couldn't make it
this is the part of code that generate the problem
$link=mysqli_connect("localhost","root","","database");
$req="SELECT DISTINCT *
FROM `qst_s`
WHERE `id_qst` = ROUND( RAND()*49 ) + 1 AND `level` = '1' LIMIT 40";
$result=mysqli_query($link,$req);
$question=$result->fetch_assoc();
I have 50 questions in my database level 1 by the way
The simplest way of selecting random rows from the MySQL database is to use "ORDER BY RAND()" clause in the query.
SELECT * FROM `table` ORDER BY RAND() LIMIT 0,1;
The problem with this method is that it is very slow. The reason for it being so slow is that MySQL creates a temporary table with all the result rows and assigns each one of them a random sorting index. The results are then sorted and returned.
Your query would look like this:
$req="SELECT DISTINCT *
FROM `qst_s`
WHERE `level` = '1'
ORDER BY RAND()
LIMIT 40";
I have a query like this,
select * from my_table where `status`='1' ORDER BY FIELD(city_id,548) ASC;
My purpose is to show all records from the table, but records with city_id=548 should come first.
Currently its showing all records but no desired sorting! Any ideas?
Actually it is working but you need to change it to DESC because FIELD() returns the index of the value in the parameter.
Other than FIELD() which accepts multiple parameters, you can alternatively use = if you only have one condition.
ORDER BY (city_id = 548) DESC
when city_id = 548 is true, it returns 1 otherwise 0 that is why we used DESC.
Try like this
descending order in sql is denoted by DESC keyword I think other values are smaller than 548 that's why it's not work as you expect
select * from my_table where `status`='1' ORDER BY FIELD(city_id,548) DESC;
I am using a PHP / MySQL pagination script which works fine, however I have trouble sorting the output accordingly.
Here parts of my script:
$link=mysql_connect("localhost","x","x");
mysql_select_db("x",$link);
$q="select count(*) \"total\" from entries";
$ros=mysql_query($q,$link);
$row=(mysql_fetch_array($ros));
$total=$row['total'];
$dis=3;
$total_page=ceil($total/$dis);
$page_cur=(isset($_GET['page']))?$_GET['page']:1;
$k=($page_cur-1)*$dis;
$q="select * from entries limit $k,$dis";
$ros=mysql_query($q,$link);
while($row=mysql_fetch_array($ros))
{
I've tried to change the line
$q="select count(*) \"total\" from entries";
to
$q="select count(*) \"total\" from entries id DESC";
however it does not seem to be working correctly.
Anyone has an idea how I can get this fixed and have the items sorted by ID?
Some expert help would be greatly appreciated - thank you very much.
Please find the original script I have in place here: http://allitstuff.com/php-mysql-pagination-script-download/
The coury
select count(*) \"total\" from entries
only returns the count. You can get only the total number of rows.
If you want to retrieve data please change your query.
If you want to manipulate with the count only, then try
$row=mysql_fetch_row($ros);
$total=$row[0];
Edit:
The OP want to sort the entries in a reverse order. So try
select column_name from entries order by column_name desc;
try it
$q="select count(*) as total from entries ORDER BY id DESC;";
Try this:
count(*) returns only count. FOR EXAMPLE
SELECT column_name1,column_name2,count(*)
FROM table_name
ORDER BY column_name1,column_name2 ASC|DESC;
This is the sql code i am running:
SELECT * FROM documents WHERE cat=1 OR cat=3 AND date>=2001 AND date<=2003 AND run <90 ORDER BY RAND() LIMIT 0,10
It should return all entries from cat 1 or cat 3 between dates 2001-2003 where run is less than 90 in a random way with a limit of 10 results.
Problem is its ignoring the date and run parameter, they work if you run everything seperate, but all together like above it only returns 10 random entries from cat 1 and 3 with any date/run..
Any idea why??
Thanks.
It may be a case of needing brackets around the OR statements like so;
SELECT * FROM documents WHERE (cat=1 OR cat=3) AND date>=2001 AND date<=2003 AND run <90 ORDER BY RAND() LIMIT 0,10
SELECT *
FROM documents
WHERE (cat=1 OR cat=3)
AND date>=2001
AND date<=2003
AND run <90
ORDER BY RAND()
LIMIT 0,10