Solved

php mysql select question

Posted on 2014-11-13
6
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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 110

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

628 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