简体   繁体   中英

SQL DISTINCT clause with WHERE

I have a MySQL database table where I can see what users are subscribed to which newsletter, it looks like this:

ID  Klant_ID Mail_ID Status Datum
2   6        1       test   test
72  6        1       10     20-03-2013
73  6        2       90     20-03-2013
74  6        4       10     20-03-2013
75  6        5       90     20-03-2013
76  8        1       10     20-03-2013
77  8        2       10     20-03-2013
78  8        4       90     20-03-2013
79  8        5       90     20-03-2013
80  6        2       90     21-03-2013
81  6        4       10     21-03-2013

I am currently using this SQL query to select all the rows with the right Klant_ID:

"SELECT * FROM Subscriptions WHERE Klant_ID = '".$_GET["ID"]."'";

Which will display this:

在此处输入图片说明

I am trying to get a query that will only display rows with a different Mail_ID and only the last ones. So basicly I want a query which will display this output:

ID Klant_ID Mail_ID Status Datum
72 6        1       10     20-03-2013
80 6        2       90     21-03-2013
81 6        4       10     21-03-2013
65 6        5       90     20-03-2013

I have tried some things using DISTINCT but I just can't get it to work properly since I haven't got that much experience with this. If you have any more questions just ask them in the comments. Any help would be great!

this should do this, depends on your DBMS

SELECT MAX(ID), Mail_ID FROM Subscriptions
    WHERE Klant_ID = '".$_GET["ID"]."' GROUP BY Mail_ID

then you have the right IDs, futher more you can get the rest information by

SELECT * FROM Subscriptions,
    (SELECT MAX(ID) as ids, Mail_ID FROM Subscriptions
        WHERE Klant_ID = '".$_GET["ID"]."' GROUP BY Mail_ID) table2
    WHERE ID=table2.ids

You didn't specify a DBMS, so this is ANSI SQL:

select id, 
       klant_id,
       mail_id, 
       status,
       datum
from (
  select id, 
         klant_id,
         mail_id, 
         status,
         datum,
         max(datum) over (partition by mail_id) as max_datum
   from Subscriptions 
   where Klant_ID = 42 --- pass your parameter here
) t
where datum = max_datum;

Please try this one

select * from Subscriptions where ID in (Select MAX(ID) from Subscriptions sub WHERE Klant_ID = '".$_GET["ID"]."' group by Mail_ID)

Distinct will not give you the result you want, because it will give you distinct rows depending on ALL the data that comes back from you query. Because you can have multiple subscriptions to the same newsletter with different dates and statuses you need to modify your query a bit more, I think aggregating by the Status and Datum and grouping by the Mail_ID might help:

"SELECT
        MAX(ID), Klant_ID, Mail_ID, MAX(Status), MAX(Datum)
    FROM Subscriptions
    WHERE Klant_ID = '".$_GET["ID"]."'
    GROUP BY Mail_ID";

Two more things, you hold data that could be of different types in your Status and Datum columns, this could be a pain if your database becomes more complex, and just concatenating an sql query string with Http-Get-Data may feel ok for testing purposes but you make yourself vulnerable to SQL-Injections, don't just pass user data unchecked to your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM