Jump to content


Photo

MySQL Queries

mysql

  • Please log in to reply
7 replies to this topic

#1 DavidKing0r

DavidKing0r

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 17 May 2013 - 11:39 AM

Hi. I have a rather large database and i require to search 'Short Item Name' for keywords (2/3 words). Not all 3 words need to be in each result. Also I'd like to know how I would search 'Number' for a partial match so if one of the records 'Number' was 098214984212 and I entered 4984212 (last 7) into a form how would i retrieve that record?

 

Thanks really appreciate if anyone helps me out.

 

dave



#2 jcbones

jcbones

    Advanced Member

  • Gurus
  • 2,439 posts
  • LocationNorth Carolina

Posted 17 May 2013 - 12:59 PM

LIKE using the wildcard (%) is what you are looking for.



#3 DavidKing0r

DavidKing0r

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 17 May 2013 - 01:37 PM

Thanks i gathered that having a bit of trouble now though. Heres what I have in my Table for example.

 

No             | Short Name        | Long Name 
21421414 | Treble blah tree | Treble tree with a blah ring

52352352 | wqe weqwe         | wqe weqwe weqrqwr rwqr qw

 

Im now trying to search the short name with 

 

$query = "SELECT * FROM DB WHERE 'Short Item Name' LIKE '%$searchTerm1%' AND '%$searchTerm2%'";

 

 

However it doesnt appear to work any ideas?


Edited by DavidKing0r, 17 May 2013 - 01:41 PM.


#4 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,384 posts
  • LocationMississauga, Canada

Posted 17 May 2013 - 02:22 PM

Is there a table named - WHERE 'Short Item Name' ?

 

If does, why did you quote it?


Edited by jazzman1, 17 May 2013 - 02:30 PM.


#5 DavidKing0r

DavidKing0r

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 17 May 2013 - 03:27 PM

The table is called DB



#6 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,384 posts
  • LocationMississauga, Canada

Posted 17 May 2013 - 03:32 PM

And the columns?



#7 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,384 posts
  • LocationMississauga, Canada

Posted 17 May 2013 - 03:37 PM

The right syntax should be something like:

SELECT * FROM tbl_name WHERE column_name_1 LIKE '%a%' AND column_name_2 LIKE '%b';

Also, don't quote the tables and columns it's a wrong sql syntax.

 

Sorry, I am in a hurry.


Edited by jazzman1, 17 May 2013 - 03:39 PM.


#8 dalecosp

dalecosp

    Advanced Member

  • Members
  • PipPipPip
  • 300 posts
  • LocationJoplin, MO

Posted 17 May 2013 - 04:36 PM

To elaborate a tad further, you often see this:

select `something` from `someplace` where `some_condition` = 1234;
Those are "backticks", and people that use them use them in order to make sure that their SQL server, (MySQL in this case, and especially) doesn't choke on any terms.

For example, you can use a reserved word as a column name, but only if you enclose it in backticks. I tend NOT to use them, and to (properly, I believe) refrain from using SQL Reserved Words as column names.

HTH,
"God doesn't play dice" --- Albert Einstein
"Perl is hardly a paragon of beautiful syntax." --- Weedpacket




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com