Obtenga un resultado basado en los servicios verificados en forma uniendo dos tablas en MySQL

I have two tables, first is user table with user details and second is services table with service id stored into it.

Usuarios de mesa:

 id    Name   mail             gender
-----------------------------------------
  1    John   john@gmail.com   Male 
  2    Mike   mike@gmail.com   Male
  3    Duke   duke@gmail.com   Male  
  4    Queen  queen@gmail.com  Female

Table Services:

 id   profile_email   service
 ----------------------------
 1    john@gmail.com    3   
 2    john@gmail.com    4    
 3    mike@gmail.com    3
 4    mike@gmail.com    5   
 5    queen@gmail.com   3
 6    queen@gmail.com   4

Form Checkboxes:

<input type="checkbox" value="1" name="services[]" id="services1"  /> Service 1
<input type="checkbox" value="2" name="services[]" id="services2"  /> Service 2
<input type="checkbox" value="3" name="services[]" id="services3"  /> Service 3
<input type="checkbox" value="4" name="services[]" id="services4"  /> Service 4
<input type="checkbox" value="5" name="services[]" id="services5"  /> Service 5

Now my question is how can i join these two table to show filtered record based on checked service.

I am trying the below query but the problem is showing duplicate row. Please help me. Thank you so much in advance.m

$select  =  "SELECT a.*,  b.services FROM users a LEFT JOIN services AS b ON b.profile_email = a.mail ";

if(isset($_REQUEST['services']))
{
$services_search = mysql_real_escape_string(implode(', ', $_REQUEST['services']));
$select .= " AND b.services IN ($services_search)";
}

preguntado el 09 de septiembre de 13 a las 21:09

2 Respuestas

Im not entirely sure what you are going to do with those tables but you shouldnt store the same adress twice. Make a new table and save just the E-Mail adresses in there and then refer to them in your tables with a foreign key.

Persons table

id    name   mail_id   gender
-----------------------------------------
 1    John   1         Male 
 2    Mike   2         Male
 3    Duke   3         Male  
 4    Queen  4         Female

Not sure what this is table

id   mail_id         service
----------------------------
1    1               3   
2    1               4    
3    2               3
4    2               5   
5    4               3
6    4               4 

Adress table

id    adress
----------------------
1     john@gmail.com
2     mike@gmail.com
3     duke@gmail.com
4     queen@gmail.com

No you can join the adress on wherever you use the foreign key of its id. Something like this:

SELECT a.name, a.gender, b.adress
FROM persons as a
INNER JOIN adress as b on a.mail_id = b.id;

http://dev.mysql.com/doc/refman/5.7/en/example-foreign-keys.html

Respondido el 09 de Septiembre de 13 a las 22:09

In the end of your query you need to group the results as there is one -to- many relation between two tables

$select  =  "SELECT a.*,  b.services FROM users a LEFT JOIN services AS b 
ON a.mail  = b.profile_email";

if(isset($_REQUEST['services']))
{
$services_search = mysql_real_escape_string(implode(', ', $_REQUEST['services']));
$select .= " AND b.services IN ($services_search)";
}

$select .= " GROUP BY  a.mail";

Respondido el 09 de Septiembre de 13 a las 21:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.