Jump to content

one sql statment


jonniejoejonson

Recommended Posts

+----+-------+----------+--------+-----------+

| id | netId | optionId | userId | value    |

+----+-------+----------+--------+-----------+

|  1 |  1    |    1    |      1 | dog      |

|  2 |  1    |    2    |      1 | cat      |

|  3 |  1    |    3    |      5 | dog      |

|  4 |  1    |    4    |      1 | dog      |

|  5 |  1    |    4    |      9 | rabbit    |

|  6 |  1    |    5    |      1 | fox      |

|  7 |  2    |    6    |      2 | dog      |

+----+-------+----------+--------+-----------+

 

 

Hello this may seem like a complicated and stupid question,

but using some ot the data above as an example...

 

i want to select unique userId's of the rows where

 

netId=1

AND

(optionId=1 AND value='dog')

AND

(optionId=2 AND value='cat')

 

 

 

 

can i do this in one select statement?...

or must i do it in two select statements and then

discard the results that dont fit my statement.

regards J.

Link to comment
https://forums.phpfreaks.com/topic/150662-one-sql-statment/
Share on other sites

Hello Festy, thanks for your continued support, however the statment deosnt pull the rows that are expected... if you imagine a database with just 2 rows and the following data

 

| id | netId | optionId | userId | value    |

+---+------+--------+--------+---------+

|  1 |  1    |    1      |      1  |  dog      |

|  2 |  1    |    2      |      1  |  cat      |

 

your sql statment, should pull out the one distinct result:  userId=1

however it is not pulling out any results.

if you change it for an OR instead of AND... then it pulls out userId=1,

however I dont want an OR i need and AND...

anyway i think you know what i mean... thanks again for your help.

I think i need to use a GROUPBY userId but i cant get my brain round it cos im

fairly new to this...

reagrds J

Link to comment
https://forums.phpfreaks.com/topic/150662-one-sql-statment/#findComment-792780
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.