Web Design Forum: Mysql recordset problem - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Mysql recordset problem checking same field in query Rate Topic: -----

#1 User is offline   Paul Boreham 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 97
  • Joined: 19-June 08
  • Reputation: 0
  • Location:Colchester, Essex
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 04 August 2009 - 02:01 PM

Right - gonna try again and close my previous post as it was too confusing! :)

Simple explanation...

====

I need to query a table to get all the records out that have 2 or more matching values in the same field.

So in english...

"Show me all records that have an ATTRIBUTE of "red" AND an ATTRIBUTE of "large"

So for example, if Product A is red and large, it would show. If product B is large and blue, it wouldnt show.

====

Is that possible in any way? Tried JOINS, UNIONS etc though I'm not advanced enough in mysql to fully understand it.

Thanks for any help/suggestions.
0

#2 User is offline   morgano 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 118
  • Joined: 10-October 08
  • Reputation: 18
  • Gender:Male
  • Location:Wales
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 04 August 2009 - 02:17 PM

$query="SELECT * FROM table WHERE ATTRIBUTE='red' AND ATTRIBUTE='large'";
0

#3 User is offline   Paul Boreham 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 97
  • Joined: 19-June 08
  • Reputation: 0
  • Location:Colchester, Essex
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 04 August 2009 - 02:21 PM

View Postmorgano, on 04 August 2009 - 02:17 PM, said:

$query="SELECT * FROM table WHERE ATTRIBUTE='red' AND ATTRIBUTE='large'";


Thanks, but that would fail.

ATTRIBUTE is 1 column - so (in this context) in cannot be red AND large - its not possible.

This feature is possible though as it works on Ebay, Amazon, Halfords etc.
0

#4 User is offline   scaz182 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 365
  • Joined: 01-April 08
  • Reputation: 2
  • Gender:Male
  • Location:Reading UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 04 August 2009 - 02:21 PM

Its a little hard to help without knowing the structure of your database but assuming the required data is in just one table surely you can just to this:

SELECT * FROM table1 WHERE attribute_1 = 'red' AND attriute_2 = 'large'

0

#5 User is offline   scaz182 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 365
  • Joined: 01-April 08
  • Reputation: 2
  • Gender:Male
  • Location:Reading UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 04 August 2009 - 02:25 PM

View PostPaul Boreham, on 04 August 2009 - 02:21 PM, said:

Thanks, but that would fail.

ATTRIBUTE is 1 column - so (in this context) in cannot be red AND large - its not possible.

This feature is possible though as it works on Ebay, Amazon, Halfords etc.


opps just saw this:

Surely you shouldn't have a database with a field that can contain multiple values (i.e. large or small and red or blue) you need to normalise it.
0

#6 User is offline   NeRo 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 824
  • Joined: 28-September 07
  • Reputation: 39
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 04 August 2009 - 03:03 PM

Is this not a case for using aliases?

SELECT * 
FROM table AS t1 , table AS t2
WHERE t1.attribute LIKE '%red%' AND t2.attriute LIKE '%large%'


It is ages since I've done this sort of thing, so it's probably wrong, but may point someone in the correct direction
0

#7 User is offline   Paul Boreham 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 97
  • Joined: 19-June 08
  • Reputation: 0
  • Location:Colchester, Essex
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 04 August 2009 - 09:28 PM

View PostNeRo, on 04 August 2009 - 03:03 PM, said:

Is this not a case for using aliases?

SELECT * 
FROM table AS t1 , table AS t2
WHERE t1.attribute LIKE '%red%' AND t2.attriute LIKE '%large%'


It is ages since I've done this sort of thing, so it's probably wrong, but may point someone in the correct direction


So, can I call the same table name and give it an alias - i.e.

FROM tableAttribute AS t1 , tableAttribute AS t2

?? Didnt know you could do that if so and may help - thanks!

Thanks for all the other replies above too, really helpful!
0

#8 User is offline   NeRo 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 824
  • Joined: 28-September 07
  • Reputation: 39
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 05 August 2009 - 01:07 PM

I tweaked the code from yesterday and tried it on one of my databases and got the desired result ..

SELECT  distinct t1.attribute 
FROM `table` AS t1 , `table` AS t2
WHERE t1.attribute LIKE '%red%' and t2.attribute LIKE '%large%'

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users