473,320 Members | 1,978 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,320 software developers and data experts.

Comparing two mysql tables, please help!

I have a website which puts customers into different groups, depending
on the referrer they can see certain products. So for example if the
referrer code = 1, they'll only be able to see the products in group
1.

To achieve this I've used 2 database tables, one for all of the
products, and one for the groups (AGProds) which contains 2 colums
(GroupID and ProdID) the system works well and I've been happy with
it.

My problem is updating the products at the moment, they have to be done
one by one, what I'd really like is a page that displayed all the
products on one page, if the product was in the particular group, it
would put a tick in the tickbox, if not it would be left blank.

What I'm really struggling with is thinking of an efficient SQL call
to the 2 tables, I basically want to compare two tables, displaying all
of the products and flagging them if they're in another table.

Could someone help? Sorry for the long winded question.

Regards

Ian

Sep 26 '05 #1
2 2797
In article <11*********************@g44g2000cwa.googlegroups. com>,
"Ian N" <ia*******@gmail.com> wrote:
I have a website which puts customers into different groups, depending
on the referrer they can see certain products. So for example if the
referrer code = 1, they'll only be able to see the products in group
1.

To achieve this I've used 2 database tables, one for all of the
products, and one for the groups (AGProds) which contains 2 colums
(GroupID and ProdID) the system works well and I've been happy with
it.

My problem is updating the products at the moment, they have to be done
one by one, what I'd really like is a page that displayed all the
products on one page, if the product was in the particular group, it
would put a tick in the tickbox, if not it would be left blank.

What I'm really struggling with is thinking of an efficient SQL call
to the 2 tables, I basically want to compare two tables, displaying all
of the products and flagging them if they're in another table.

Could someone help? Sorry for the long winded question.


I would solve it with two SQL queries, like this:

<?
$q=mysql_query("select * from AGProds") or print mysql_error();
while ($r=mysql_fetch_array($q)){
$groups[$r["ProdID"]][] = $r["GroupID"];
}

$q=mysql_query("select * from Products") or print mysql_error();
while ($r=mysql_fetch_array($q)){
$g1 = in_array(1, $groups[$r["id"]]) ? "[X]" : "[ ]";
$g2 = in_array(2, $groups[$r["id"]]) ? "[X]" : "[ ]";
print "$r[name] - $g1 - $g2\n";
}
?>

It's dirty, but you may get the general idea. That would output something like:

Hair gel [X] [ ]
Shave gel [ ]*[X]

And so on. You should adjust it to fit your output of course.

Now, if you want to select all products that only exist in group 2 - you do it
like this:

<?
$q=mysql_query("select * from Products,AGProd where AGProd.ProdID =
Product.id and AGProd.GroupID = 2") or print mysql_error();
while ($r=mysql_fetch_array($q)){
print "$r[name] - $r[GroupID]\n";
}
?>

--
Sandman[.net]
Sep 26 '05 #2
Thanks Sandman, i'll try and implement this now.

Cheers,

Ian

Sep 27 '05 #3

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

Similar topics

0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
0
by: Girish Agarwal | last post by:
--0-474210375-1058976151=:31789 Content-Type: text/plain; charset=us-ascii Content-Id: Content-Disposition: inline Note: forwarded message attached. __________________________________
0
by: Anil Garg | last post by:
Hi, I am trying to install myql on redhat. mysql package used: mysql-3.23.52-unknown-freebsdelf4.6-i386.tar.gz While following the instructions in install document..when i run the follwing...
0
by: Mark Adams | last post by:
I really need some help with this. MySQL will not start on boot despite everything I've done to make sure that it is set to do so. When I start it as root from a terminal with...
4
by: Adam Smith | last post by:
I have a dedicated server running 'FreeBSD 4.9 STABLE' at a hosting site. They have done some default installations, presumably from the CVS ports package ??. Herein lies the problem, "I do not...
2
by: pc | last post by:
hi everyone, we have a server on which someone blew away the MySQL 4 progs directory before installing MySQL 5. When installing MySQL 5 the installation failed with an error stating that it was...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
1
by: PowerLifter1450 | last post by:
I've been having a very rough time installinig mySQL on Linux. I have been following the instructions form here: http://www.hostlibrary.com/installing_apache_mysql_php_on_linux Everytime I get to...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.