mysql - SELECT data filtering by 2 values -
long time since last time... problem here that: 1 table (user) users data (name, phone etc) other table (user_type) have , id , varchar type (student,worker etc) user has relationship 1-n user_type_user. user_type_user has relationship 1-n user_type. can guess, user_type_user stores users' ids , user_type id's. 1 user can of 1 or more types.
my problem here want select user data , user type users type 1 , 2. have (sorry, names in spanish).
select socio.*, socio_tipo_socio.id_tipo_socio,tipo_socio.tipo ((respeta.socio inner join respeta.socio_tipo_socio on socio.dni = respeta.socio_tipo_socio.socio_dni) inner join respeta.tipo_socio on tipo_socio.id_tipo_socio = socio_tipo_socio.id_tipo_socio) socio_tipo_socio.id_tipo_socio = 1 , socio_tipo_socio.id_tipo_socio = 2
this doesn't work. know can place or instead and, give me users type 1 or 2.
any ideas? hope data base professor don't see i'm asking xd. thanks
edit 1. way of make query (i ask less data), maybe helps understand i'm trying accomplish. have user1 type 1 , 2, , user2, type 2. want retrieve users type1 , type2, so, ideal query return user1.
select distinct(socio.dni), socio.nombre socio, socio_tipo_socio, tipo_socio socio.dni = socio_tipo_socio.socio_dni , socio_tipo_socio.id_tipo_socio = tipo_socio.id_tipo_socio , tipo_socio.id_tipo_socio = 2
obviously, query above, shows type2 users (user1 , user2). don't know how fix "and tipo_socio.id_tipo_socio = 2 , and tipo_socio.id_tipo_socio = 1" , have user1. that's i'm stuck.
edit 2
solved!!!! yeyyy!!!
select * socio, tipo_socio, socio_tipo_socio tipo_socio.id_tipo_socio = 1 , socio.dni in ( select socio.dni socio, socio_tipo_socio, tipo_socio tipo_socio.id_tipo_socio = 2 , tipo_socio.id_tipo_socio = socio_tipo_socio.id_tipo_socio , socio.dni = socio_tipo_socio.socio_dni ) , tipo_socio.id_tipo_socio = socio_tipo_socio.id_tipo_socio , socio.dni = socio_tipo_socio.socio_dni
what have done there logically correct. none of 1 single record can have value 1 , value 2 simultaneously. should finding records type1, finding records type2 , merging them.
rows type1
select * users utu types typeid = 1 , <table connectors>
rows type2
select * users utu types typeid = 2 , <table connectors>
rows type1 if in type2 also
select * users typeid = 1 , in ( select * users utu types typeid = 2 ) , <table connectors>
edit: detailed version.
no. need use this.
select * users typeid = 1 , in userid ( select userid users utu types typeid = 2 ) , tipo_socio.id_tipo_socio = socio_tipo_socio.id_tipo_socio , socio.dni = socio_tipo_socio.socio_dni
first table founds people type1.second findind type2. see th in in 3rd query means find people type1 , in type2. array search.
Comments
Post a Comment