Nati323 Posted March 31, 2015 Share Posted March 31, 2015 (edited) sorry on the topic, i dont know how to explain my self . lets assume that i have table like this: ID MONTH------------------------------- 1 | 1 2 | 1 3 | 2 now , i want to select the all rows BUT only one from every MONTH, i mean the result should be: ID MONTH------------------------------- 1 | 1 3 | 2 the row with id 2 , dosent get in the result because the MONTH column of ID 2 equal to MONTH in ID 1. how can i do this? if you dont understand me please say and i will try to explain my self better. Edited March 31, 2015 by Nati323 Link to comment Share on other sites More sharing options...
Ingolme Posted March 31, 2015 Share Posted March 31, 2015 You can wither SELECT DISTINCT month or you can GROUP BY month. Link to comment Share on other sites More sharing options...
Nati323 Posted March 31, 2015 Author Share Posted March 31, 2015 ok a few que's, when i use DISTINCT, like this for example: SELECT DISTINCT COL1, COL2 the COL2 will be DISTINCT to? next que i wondering how i do the next one, i have a table with YEAR col and MONTH col look like this: YEAR - MONTH------------ 2015 | 3 2014 | 3 now, if i do DISTINC to the MONTH column i get only one year, but i want both and if i do DISTINC to the YEAR col so i get only one month from every year how can i do that i get from every year distincs months? i never understand GROUP BY :| Link to comment Share on other sites More sharing options...
justsomeguy Posted March 31, 2015 Share Posted March 31, 2015 the COL2 will be DISTINCT to?Try it and see what happens. Link to comment Share on other sites More sharing options...
Nati323 Posted April 1, 2015 Author Share Posted April 1, 2015 (edited) ok i try it its now its not affected the second column, what about my other question? and how can i DISTINCT two columns? Edited April 1, 2015 by Nati323 Link to comment Share on other sites More sharing options...
justsomeguy Posted April 1, 2015 Share Posted April 1, 2015 Distinct means that it will return distinct records. If you say distinct year, month then it will return records that have a distinct combination of the 2 columns. That means that you might have 2 records that have 2014 if the months are different, or you might have 2 records that have month 3 if the years are different, but you won't have 2 records that have the same month and year. If you want a list of distinct years, and also a list of distinct months, then you need to run 2 queries. Link to comment Share on other sites More sharing options...
Nati323 Posted April 3, 2015 Author Share Posted April 3, 2015 amm, about 2 queris , this not what i want, i dont want list of distinct years alone and list of distinct months alone, i want a combination like you say in the start of your msg, how can i do that? because from what i saw its not possible to distinct two columns in one query... Link to comment Share on other sites More sharing options...
justsomeguy Posted April 6, 2015 Share Posted April 6, 2015 Using distinct will cause the entire result set to be distinct. If you select 2 columns, then they will be distinct, you won't get a result that has 2 records with the same values in each column. That's just a normal distinct query. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now