Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

mySql query

PHP MySql web application:
This seems simple but I can't figure out how to do it. I need to search an indexed table on an indexed column for an "exact match".  Then I need to start displaying data from that point on for n more rows.

Example:
This works because it's an "inexact match" and there is at least one more match left:
mysql>  select name from testbed where name like 'br%' limit 2;
+----------+
| name     |
+----------+
| Bradshaw |
| Brown    |
+----------+
2 rows in set (0.00 sec)


This doesn't work because it has satisfied it's match.  I want to keep listing from this point for n more rows:
mysql>  select name from testbed where name like 'bradshaw' limit 2;
+----------+
| name     |
+----------+
| Bradshaw |
+----------+
1 row in set (0.00 sec)


This is what I want:
mysql>  select name from testbed where name like 'bradshaw' (keep listing for one more);
+----------+
| name     |
+----------+
| Bradshaw |
| Collins |
+----------+
1 row in set (0.00 sec)


Thank you for you help.
0
ibanja
Asked:
ibanja
  • 2
1 Solution
 
jeffparisCommented:
For an exact match, just do this:


SELECT name FROM testbed WHERE name = 'bradshaw' limit 2;






Just for an extra info, here is what I have for one of my blogs search tools:

$query = 'SELECT u.username, u.user_id, u.category, b.blog_id, b.date, b.subject, b.comments, b.views FROM table AS u LEFT JOIN table AS b ON  u.user_id = b.author_id WHERE b.body LIKE "%' . addslashes(trim($phrase)) . '%" AND u.category != 0 ORDER BY b.date DESC LIMIT ' . $start . ', ' . $end;
$fresult = mysql_query($query);
$total_results = mysql_num_rows($fresult);
0
 
jeffparisCommented:
In posting that, I found a flaw or two. But, use this instead and if the user wants to use WILDCARDS and not exact matches, inform your users to place a % before and after the keywords:)

Here it is:


$query = 'SELECT name FROM table WHERE name LIKE ' . addslashes(trim($search_phrase)) . ' ORDER BY b.date DESC LIMIT 2;
$fresult = mysql_query($query);
$total_results = mysql_num_rows($fresult);


// $search_phrase is the search phrase variable
0
 
_kiewCommented:
Have you tried :

select name from testbed where name >= 'bradshaw' order by name limit 2;

?
0
 
ibanjaAuthor Commented:
kiew,

> select name from testbed where name >= 'bradshaw' order by name limit 2;

That's it!  Exactly what I was looking for.

Thanks to jeffparis as well.  Sorry I didn't respond back right away. I got sidetracked with some non-programming issues.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now