473,545 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using DISTINCT to get unique results

258 Contributor
I'm using DISTINCT to get one of each title out of my table.

Here is my table:

Expand|Select|Wrap|Line Numbers
  1. +---------------------------+
  2. |    title     |  weather   |
  3. +---------------------------+
  4. |   Monday     |    Good    |
  5. |   Tuesday    |    Bad     |
  6. |   Wednsday   |    worst   |
  7. |   Tuesday    |    Good    |
  8. +---------------------------+
  9.  
Query =
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT title , weather FROM mytable
And my question is:
in this case there is 2 rows with same title (Tuesday). so just one of them will be showed but we also have 2 rows with same weather ( Good ) which dont have same title... I need to know if only one of these three rows will be showed or 2?
What I mean is that I need only unique titles not unique weather...
Sep 24 '07 #1
8 11973
pbmods
5,821 Recognized Expert Expert
Heya, bnashenas.

Please use CODE tags when posting source code:

[CODE=MySQL]
MySQL code goes here.
[/CODE]

Use a GROUP BY clause:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     DISTINCT
  3.         `title`,
  4.         `weather`
  5.     FROM
  6.         `mytable`
  7.     GROUP BY
  8.         `title`,
  9.         `weather`
  10.  
Sep 24 '07 #2
bnashenas1984
258 Contributor
What exactly does it do? I mean GROUP BY
Sep 25 '07 #3
r035198x
13,262 MVP
What exactly does it do? I mean GROUP BY
Read all about it here.
Sep 25 '07 #4
bnashenas1984
258 Contributor
Thanks guys

It worked
Sep 25 '07 #5
pbmods
5,821 Recognized Expert Expert
Heya, Bnashenas.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Sep 25 '07 #6
bnashenas1984
258 Contributor
Sorry guys
I did all you said but I think I'm still having problem..

What i need is that I want DISTINCT to take effect only on 1 Column ( title ) and I also need to get all rows...

Check this:

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT title FROM mytable 
  2.  
But the problem is that this command only returns title column... I mean if i use
Expand|Select|Wrap|Line Numbers
  1. $result=mysql_fetch_array();
  2.  
in php i only get result[title] but i need result[weather] too
As you said I also used GROUP BY and i thought it was working but it wasn't

Thanks agin for your replies
Sep 27 '07 #7
pbmods
5,821 Recognized Expert Expert
Heya, Bnashenas.

What's your query look like?
Sep 27 '07 #8
bnashenas1984
258 Contributor
Hello pbmods...
Thanks for your replies.. Actually i solved the problem by using this command :

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM mytable GROUP BY title,weather
  2.  
Thanks again
Sep 28 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2469
by: Alex Satrapa | last post by:
I have a table from which I'm trying to extract certain information. For historical reasons, we archive every action on a particular thing ('thing' is identified, funnily enough, by 'id'). So the only way to find out the current state of a particular combination of attributes is to "select distinct on (id, ...) ... order by date desc". In...
9
1780
by: LarryR | last post by:
The following XSLT works fine using MSXML 4.0 (e.g I receive a result in about 20 seconds), but effectively hangs in both .NET 1.0 sp2 with the XML hot fix and NET 1.1. My source XML file is large at over 46,000 <atl_client> nodes <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0"...
5
53364
by: Fred Zuckerman | last post by:
Can someone explain the difference between these 2 queries? "Select Distinct id, account, lastname, firstname from table1" and "Select DistinctRow id, account, lastname, firstname from table1" Thanks, Fred Zuckerman
6
131111
by: zaphod | last post by:
I need to select unique combinations of 4 columns from one table and insert them into a new table but I can't think of any way of finding unique combinations of more than 1 column since SELECT DISTINCT only works on single columns. Any ideas? zaphod
8
1668
by: @sh | last post by:
This may be a really simple question, but I always have problems with Distinct queries. In this instance, I have a table of Delivery addresses, some will be exactly the same EXCEPT for the DeliveryID field, but eitherway I need the DeliveryID so that I can pass it onto the next form. So, I need to run a query that will find only addresses...
0
2830
by: NeoGeo | last post by:
I have a problem with a SQL SELECT query. As far as my research goes i figured out that UNIQUE is used when you have one column that you whant unique and DISTINCT is used when you have more than one column that you want to all be unique. But i have a query where i want just some of the columns to be unique and some not. Here is the full query...
1
3500
newnewbie
by: newnewbie | last post by:
Desperately need help in creating a query to count unique values in a table. I am a Business analyst with limited knowledge of Access….My boss got me ODBC connection to the underlying tables for our system and thinks I am omnipotent now and can extract any data out of it in the form he wants….The truth is, though I know SOME Access, I am not a...
6
9025
by: shira | last post by:
Hi, Looking to see if someone might have an explanation for this behavior. Is it a bug? Corruption? I have been able to reproduce the problem with only 2 rows and 1 field. Here is the table: Field1 231.2
4
3701
by: omnittha | last post by:
I would like to pull unique records of a SINGLE column but not the other related columns. When I use DISTINCT, it pulls up all the records which are unique across the columns. Here is the code.... SELECT distinct columnA,columnB,columnC FROM Table ORDER by columnA,columnB,columnC; What I would like to see, is Distinct records from column...
0
7487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7934
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6003
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.