Current location: Hot Scripts Forums » Programming Languages » PHP » where clause problem!, multiple condition problem


where clause problem!, multiple condition problem

Reply
  #1 (permalink)  
Old 03-25-06, 04:12 PM
djnaf's Avatar
djnaf djnaf is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
where clause problem!, multiple condition problem

hello;

i have this table (restcards_tbl) ,
both are primary keys
+--------+--------+
| restid | cardid |
+--------+--------+
| 1 | 1 |
| 1 | 2 |
| 1 | 4 |
| 1 | 6 |
| 15 | 1 |
| 15 | 2 |
| 15 | 3 |
| 15 | 4 |
| 15 | 12 |
+--------+--------+

and this table (cards_tbl)

+--------+------------------+
| CardID | CardName |
+--------+------------------+
| 1 | Visa |
| 2 | Master Card |
| 3 | American Express |
| 4 | GO Card |
| 5 | Golden Card |
| 6 | Green Card |
| 7 | Silver Card |
| 12 | Bronze Card |
+--------+------------------+

every restaurant have an id RESTID.
and CARDID means , the id of the cards accepted in the restaurants.

i want a coide that the user can search for the restaurants that accepts for example
Visa AND MasterCard AND American Express

and it displayes the id for those restaurants!

i tried
PHP Code:

select restid from restcards_tbl where cardid=and cardid=and cardid=
but no results
if any1 has a php code to help me display results i'd be grateful

Thank you
__________________
Code Talking Forum
Programming & Scripts Forum
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-25-06, 06:39 PM
wiremind wiremind is offline
Newbie Coder
 
Join Date: May 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
You should be using OR instead of AND.
Code:
select restid from restcards_tbl where cardid=1 OR cardid=2 OR cardid=3
Or you could do it like this
Code:
select restid from restcards_tbl where cardid IN (1, 2, 3)
__________________
Online Dating Software
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-25-06, 07:11 PM
djnaf's Avatar
djnaf djnaf is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
OR won't work , i want all 3 together

and IN will display all restaurants that has 1 alone and 2 and 3
not the restaurant that has 1,2,3 together!
__________________
Code Talking Forum
Programming & Scripts Forum
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 03-25-06, 07:37 PM
stormshadow's Avatar
stormshadow stormshadow is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 355
Thanks: 0
Thanked 0 Times in 0 Posts
Okay what i would do is only have 1 table...

Resturants table or w.e.

PHP Code:

CREATE TABLE `resturants` (

`
restidINT255 NOT NULL AUTO_INCREMENT ,
`
VisaCHAR) DEFAULT 'N' NOT NULL ,
`
Master CardCHAR) DEFAULT 'N' NOT NULL ,
`
American ExpressCHAR) DEFAULT 'N' NOT NULL ,
`
GO CardCHAR) DEFAULT 'N' NOT NULL ,
`
Golden CardCHAR) DEFAULT 'N' NOT NULL ,
`
Green CardCHAR) DEFAULT 'N' NOT NULL ,
`
Silver CardCHAR) DEFAULT 'N' NOT NULL ,
`
Bronze CardCHAR) DEFAULT 'N' NOT NULL ,
PRIMARY KEY ( `restid` )
TYPE MYISAM 
Then when you insert the restraunt change the cc type from N to Y

to find do...

mysql_query("SELECT * FROM resturants WHERE `Silver Card`='Y' && `American Express`='Y'");

That would take all the resturants that accept Silver cards and american express....

I didnt read your whole ? so i hope that is what you are looking for...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 03-25-06, 08:04 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
This query will do it -

$query = 'SELECT restid, COUNT(*) AS cnt FROM restcards_tbl WHERE cardid=1 OR cardid=2 OR cardid=3 GROUP BY restid HAVING cnt = 3';

So what does this do:

1) This part - SELECT restid FROM restcards_tbl WHERE cardid=1 OR cardid=2 OR cardid=3 - gets all of restid rows where cardid is 1, 2, or 3. Note, using AND does not work as any row only has one value for cardid and the AND would require a row to have values of 1 AND 2 AND 3 at the same time, which they cannot.

2) This part - COUNT(*) AS cnt GROUP BY restid - groups the rows by restid and produces a count of the rows in each group called cnt.

3) This part - HAVING cnt = 3 - limits the results to those where cnt = 3 (there were three rows in the result, one for each cardid=) Having a value of 2 would mean only two rows matched, having a value of 1 would mean only one row matched, and zero would mean that there were no rows that matched.

The results produced by this will be a restid followed by the cnt (which will be 3)

some_restid 3
someother_restid 3
another_restid 3
.
.
.
P.S. The above can also use the cardid IN (1, 2, 3) syntax instead of the cardid=1 OR cardid=2 OR cardid=3.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???

Last edited by mab; 03-25-06 at 08:06 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 03-25-06, 08:16 PM
djnaf's Avatar
djnaf djnaf is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
MAB your amazing , and always helpful
the logic ur talking about is great!

Thanks alot for ur help
__________________
Code Talking Forum
Programming & Scripts Forum

Last edited by djnaf; 03-25-06 at 08:23 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Date/Time format & multiple selection with listbox problem in ASP.net & C# frmsasp ASP.NET 0 10-13-05 07:07 AM
Preventing multiple user from loging in using the same username and password digioz PHP 2 09-14-05 02:53 PM
Search database with multiple AND problem Bonzo PHP 8 07-14-05 03:09 PM
problem with multiple file upload and loop sita12691 PHP 0 05-03-04 04:29 PM
Multiple dropdown plus link problem bastiaan JavaScript 0 11-17-03 05:22 PM


All times are GMT -5. The time now is 01:40 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.