473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

comma seperated list comparison

is it possible to compare acomma separated list aginst another

eg comma list 1 => 1,2,3,4,5
comma list 2 => 3,5
can you check that 3 is in both, and 5 is in both, therfore they match???

the comparison is to check that if product a who supplies products 1,2,3,4,5
can be used instead of product b who supplies 3,5 as product a already
supplies them


Jul 23 '05 #1
2 7700
Craig Keightley wrote:
is it possible to compare acomma separated list aginst another

eg comma list 1 => 1,2,3,4,5
comma list 2 => 3,5
can you check that 3 is in both, and 5 is in both, therfore they match???

the comparison is to check that if product a who supplies products 1,2,3,4,5
can be used instead of product b who supplies 3,5 as product a already
supplies them


Here's a MySQL solution that works for product values 0 through 99. It
uses a cartesian product to generate a list 0..99 (it's easy to add
another digit to make it 0..999). Then it uses a regular expression to
find the value in your comma-separated lists, using the MySQL regexp
token for word boundaries (to prevent "2" from matching "12" or "20").

The result set of this query is two rows, for the values that are common
to both lists: 3 and 5.

SELECT tens.d*10 + ones.d AS n
FROM (SELECT 0 AS d UNION ALL
SELECT 1 AS d UNION ALL
SELECT 2 AS d UNION ALL
SELECT 3 AS d UNION ALL
SELECT 4 AS d UNION ALL
SELECT 5 AS d UNION ALL
SELECT 6 AS d UNION ALL
SELECT 7 AS d UNION ALL
SELECT 8 AS d UNION ALL
SELECT 9 AS d) AS tens
INNER JOIN
(SELECT 0 AS d UNION ALL
SELECT 1 AS d UNION ALL
SELECT 2 AS d UNION ALL
SELECT 3 AS d UNION ALL
SELECT 4 AS d UNION ALL
SELECT 5 AS d UNION ALL
SELECT 6 AS d UNION ALL
SELECT 7 AS d UNION ALL
SELECT 8 AS d UNION ALL
SELECT 9 AS d) AS ones
WHERE "1,2,3,4,5" REGEXP CONCAT('[[:<:]]', (tens.d*10 + ones.d), '[[:>:]]')
AND "3,5" REGEXP CONCAT('[[:<:]]', (tens.d*10 + ones.d), '[[:>:]]')
ORDER BY n

(Unfortunately, we can't use the column alias 'n' in the WHERE clause.
This is a documented limitation of MySQL and the SQL language in general.)

You can replace the regexp comparison with an IN predicate if you can
interpolate your comma-separated lists as string into the SQL query:

....
WHERE (tens.d*10 + ones.d) IN ( $comma_list_1 )
AND (tens.d*10 + ones.d) IN ( $comma_list_2 )
ORDER BY n

Regards,
Bill K.
Jul 23 '05 #2

Craig Keightley wrote:
is it possible to compare acomma separated list aginst another

eg comma list 1 => 1,2,3,4,5
comma list 2 => 3,5
can you check that 3 is in both, and 5 is in both, therfore they match???
the comparison is to check that if product a who supplies products 1,2,3,4,5 can be used instead of product b who supplies 3,5 as product a already supplies them


Here's a pure PHP solution I just created:
<?
function comp_csv($s1,$s2)
{
$tmp1 = explode(',',$s1);
$tmp2 = explode(',',$s2);
$tmp3 = array_intersect($tmp1,$tmp2);
$smaller_array = (count($tmp1) < count($tmp2))?$tmp1:$tmp2;
$smaller_count = count($smaller_array);
$inboth = FALSE;
if (count($tmp3) == $smaller_count)
if (count(array_diff($tmp3,$smaller_array)) == 0) $inboth =
TRUE;
return($inboth);
}

$csv1 = '1,2,3,4,5';
$csv2 = '3,5';
$x = comp_csv($csv1,$csv2);
var_dump($x);
?>
The function:
1) creates temporary arrays from the CSV strings
2) gets the intersection between the two arrays, which should have no
more elements than the smaller array. It can have less.
3) if it has the same number of elements as the smaller array, it
checks whether the it's elements are the same as those in the smaller
array. If so, the returned value is set to TRUE

Ken
BTW, followups changed to just comp.lang.php

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Craig Keightley | last post by:
is it possible to compare acomma separated list aginst another eg comma list 1 => 1,2,3,4,5 comma list 2 => 3,5 can you check that 3 is in both, and 5 is in both, therfore they match??? the...
2
by: Pmb | last post by:
I'm trying to learn the syntax for initializing objects in a comma separated list. Below is an example program I wrote to learn how to do this (among other things). While I understand how to...
11
by: Craig Keightley | last post by:
I have a mysql database with a list of companies who supply specific products tblSuppliers (simplified) sID | sName | goodsRefs 1 | comp name | 1,2,3,4,5 2 | company 2 | 2,4
3
by: Gary Smith | last post by:
Hi, I've got a field that contains a list of rooms. In most cases, this contains a single ID. However, under some circumstances, the field may contain a list of two IDs which are broken by a...
11
by: Shawn Odekirk | last post by:
Some code I have inherited contains a macro like the following: #define setState(state, newstate) \ (state >= newstate) ? \ (fprintf(stderr, "Illegal...
3
by: dfetrow410 | last post by:
I need make a comma seperated list, but whwn I build the list I get a comma at the end. How do I remove it? foreach (ListItem lst in REIPropertyType.Items) { if (lst.Selected == true) {...
2
nehashri
by: nehashri | last post by:
i hv a database in access wid Asp as front end. ven given a word in search command(of the front end) it shud go to a table in which each field has words serated by comma. each word shud b checked...
0
by: Kristi | last post by:
I need to create a CL program that will take a PF, and create a tab delimited file that has comma seperated column headings as the first record. I know i can use cpytostmf/cpytoimpf to create the...
6
by: orajit | last post by:
Hi, I wanted to list the all columns of a emp table . The output should be comma seperated and list of column should come in brakets like (enam,sal,jdate,job). The below code gives me proper...
1
by: udaypawar | last post by:
Hi All, I have one problem here with mysql stored procedures. I have a list of ids seperated by comma e.g., (" 'A', 'B', 'C' "). I am passing the same to mysql stored procedure as a parameter....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.