Solved

php mysql select question

Posted on 2014-11-13
6
297 Views
Last Modified: 2014-11-16
Hello,

I have:
$yfrom = 1970;
$yto = 1974;

Current select snippet:
listing_title LIKE '%".$yto."%' <- this finds all of the listings with the year in the title

and I would like it to find a range, is there a way to find a range of numbers? I am guessing that I need to break it out like

listing_title LIKE '%1970%' OR listing_title LIKE '%1971%' OR listing_title LIKE '%1972%' OR listing_title LIKE '%1973%'

is there a dynamic way to do this?

Thanks,
Matt
0
Comment
Question by:movieprodw
6 Comments
 
LVL 8

Expert Comment

by:vr6r
ID: 40440547
Are the years in the titles always in the same place?
If so, you could try extracting them with a string function (eg: substring() if they're always in a certain spot in the middle of the title, or right(4) if they're always at the end, etc) and then compare against that value or try a BETWEEN() clause on that result for a range of years.
0
 
LVL 33

Assisted Solution

by:Norie
Norie earned 125 total points
ID: 40440555
You could use BETWEEN or >= and <=.
0
 
LVL 11

Assisted Solution

by:Radek Baranowski
Radek Baranowski earned 125 total points
ID: 40440996
BETWEEN will work if column has some countable data type like int or date/timestamp

For strings only LIKE might be of use or the way author suggested himself.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 8

Accepted Solution

by:
vr6r earned 125 total points
ID: 40441051
BETWEEN will evaluate strings.  

select case when right("abc1964", 4) BETWEEN "1950" AND "1970" THEN "true" else "false" end
-----
result = "true"

select case when right("abc1964", 4) BETWEEN "1950" AND "1960" THEN "true" else "false" end
-----
result = "false"

Open in new window


The real question is if the date portion of the string can be reliably extracted from the title.  If it's in a different position within the title for each record then it would be difficult.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 40443733
Can you please show us the CREATE TABLE statement and some of your test data?
0
 
LVL 1

Author Closing Comment

by:movieprodw
ID: 40446634
Thank you for the responses, it looks like I have to rebuild how the data is stored in order to properly achieve this.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now