0

I need to make somethink like a elecction system so that people at work would select an elecction and select for every person in my DB if he voten or not, and save it to another db table but if i allready selected that a person voted in a election it should not ask me again, but if i select another election it would ask again. Im using PHP and MySQL for everything.

ive had diferent queries like:

SELECT * 
FROM   personas 
          LEFT OUTER JOIN votaciones 
                 on personas.id = votaciones.id_persona 
WHERE  personas.seccion = 6  AND 
       personas.mesa LIKE 'U' AND 
       votaciones.id_eleccion IS NULL 
LIMIT 5

Currently i have 3 tables. 1 with people in it, its has its id, name, second name, and to which table he went to vote, then another table for all the diferent elecctions, like national elecction of year 2000 and last is the votes that people made.

the last one is Id_person as foreign key from peoples, id_elecction from the elecction, and vote which is 1 or 0 (yes and no).

if it was 1 election it would be easy but with many and i dont know how much becouse you can add more elections in the future.

Thank you in regards to every1 who answers with any answer.

The question: I need to think of a query that would give me names of people that still havent voted for a specific election, like all people that didnt vote in election 1. And if a person voted in election 1 and still havent voted in election 2. when i ask for election 1 that person would not apear but when i ask for election 2 he would apear as he still havent voted.

something like:

SELECT distinct id, nombre, apellido1,
       apellido2, personas.seccion 
FROM  personas 
        LEFT JOIN votaciones 
             on personas.id = votaciones.id_persona 
WHERE  personas.seccion = 1 AND 
       mesa = 'A' AND 
      (id_eleccion IS NULL OR NOT EXISTS 
                 (SELECT * 
                  FROM votaciones
                  WHERE id_eleccion = 2 )
       ) 
4

1 に答える 1

0
    SELECT distinct id,nombre, apellido1,apellido2, personas.seccion from personas
      LEFT JOIN votaciones on personas.id = votaciones.id_persona 
       WHERE personas.seccion = 1 AND mesa = 'A' 
        AND (id_eleccion IS NULL OR id 
         NOT IN (SELECT id FROM personas JOIN votaciones ON personas.id = votaciones.id_persona WHERE id_eleccion =1))

That is the conclusion i came to.

于 2012-08-02T23:32:44.737 に答える