SELECT * FROM mm_tfs
WHERE product_slug LIKE '%football%'
AND schoolid = '8' AND category_id ='21'
LIMIT 4
Returns 4 values like I'm asking, but the following statement returns 0 - is there a rule about using the OR statement that I'm not familiar with? My assumption is that it should return all of the values in 1 (or more if it weren't for the limit).
SELECT * FROM mm_tfs
WHERE (product_slug LIKE '%football%'
AND schoolid = '8' AND category_id ='21')
OR (product_description LIKE '%football%'
AND schoolid = '8' AND category_id ='21')
LIMIT 4
NOTE by cyberkiwi The first OR portion of Q2 is exactly the same as the WHERE clause on Q1
product_description LIKE '%football%' AND schoolid = '8' AND category_id ='21
Without the OR Statement by itself does produce the desired result as long as it does not have the limit. When the OR statement is used with the LIMIT as well, there are 0 values returned.
SELECT * FROM mm_tfs
WHERE product_description LIKE '%football%'
AND schoolid = '8' AND category_id ='21'
LIMIT 4
^-- That produces 0 results
SELECT * FROM mm_tfs
WHERE product_description LIKE '%football%'
AND schoolid = '8' AND category_id ='21'
^-- That produces results
The strangest part of this is that all of these queries product the right effect in my PHPMYADMIN SQL query window, but not in the application itself.
The SQL LIMIT clause constrains the number of rows returned by a SELECT statement. For Microsoft databases like SQL Server or MSAccess, you can use the SELECT TOP statement to limit your results, which is Microsoft's proprietary equivalent to the SELECT LIMIT statement.
TIP: SELECT LIMIT is not supported in all SQL databases. For databases such as SQL Server or MSAccess, use the SELECT TOP statement to limit your results. The SELECT TOP statement is Microsoft's proprietary equivalent to the SELECT LIMIT statement.
The SQL LIMIT clause restricts how many rows are returned from a query. The syntax for the LIMIT clause is: SELECT * FROM table LIMIT X;. X represents how many records you want to retrieve. For example, you can use the LIMIT clause to retrieve the top five players on a leaderboard.
SELECT TOP, LIMIT and ROWNUMThe LIMIT , SELECT TOP or ROWNUM command is used to specify the number of records to return. Note: SQL Server uses SELECT TOP . MySQL uses LIMIT , and Oracle uses ROWNUM .
You are repeating some of the conditions which is not necessary. Try this instead:
SELECT * FROM mm_tfs
WHERE
(product_slug LIKE '%football%'
OR product_description LIKE '%football%')
AND schoolid = '8'
AND category_id ='21'
LIMIT 4
UPDATE:
I have created the following table:
create table mm_tfs2 (schoolid varchar(2), categoryid varchar(2),
description varchar(20), slug varchar(20));
And 5 times:
insert into mm_tfs2 values (8, 21, '', 'football');
And finally the query:
select * from mm_tfs2 where
(slug like '%football%' and schoolid = 8 and categoryid = 21)
or (description like '%football%' and schoolid = 8 and categoryid = 21)
limit 4;
+----------+------------+-------------+----------+
| schoolid | categoryid | description | slug |
+----------+------------+-------------+----------+
| 8 | 21 | | football |
| 8 | 21 | | football |
| 8 | 21 | | football |
| 8 | 21 | | football |
+----------+------------+-------------+----------+
4 rows in set (0.00 sec)
So I'm sorry to say that I'm not able to recreate the problem.
Try to put ( ) arround the entire condition
( (...AND...) OR (...AND...) )
Like so:
SELECT * FROM mm_tfs WHERE
( (product_slug LIKE '%football%' AND schoolid = '8' AND category_id ='21')
OR (product_description LIKE '%football%' AND schoolid = '8' AND category_id
='21')
) LIMIT 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With