Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2009
    Posts
    4

    Unanswered: group by clause,mysql query on data

    Hi,

    I am using mysql query to generate a report. I would like to know can whether we can use group by clause on data itself? Let me elaborate. Let'ss asssume I have following table called employee and database is myrecords.

    ----------------------------------------------------------------------------------------------------------------------------------------------------

    id name accountno city descrption sex

    ----------------------------------------------------------------------------------------------------------------------------------------------------

    1 abc 100 NY senior executive m

    2 pqr 200 NY senior executive f

    3 xyz 300 NY senior executive m

    4 stu 400 LON technical expert f

    5 def 500 PAR senior executive m

    6 rth 600 HR m

    7 uty 700 HR f

    8 ytt 800 technical expert m

    --------------------------------------------------------------------------

    Now I want to display the records based on the description. Partiularly, I am looking for the data based on description only "senior executive" and "HR". I do not wish to take any other records. In short, I am looking for the data based on the data. Can I extract data from data? Therefore, I am using group by description clause. How can I use it?

    select employee.id,employee.name,employee.accountno,emplo yee.city,employee.description

    from employee group by employee.description order by employee.id

    Any suggestions or ideas friends?



    Best regards.

  2. #2
    Join Date
    Sep 2009
    Location
    San Sebastian, Spain
    Posts
    880
    Check out SELECT DISTINCT this might help you without using GROUP BY. For example if you have a table with a column and you only want to get the unique entries for this column then SELECT DISTINCT fieldname FROM table will return these results.

    The GROUP BY clause in a select can be used to generate a similar type result, however, it is more powerful because it can also return to you more information about what you are grouping on. Here is an online tutorial about GROUP BY SQL GROUP BY - SQL Tutorial
    Ronan Cashell
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    http://www.it-iss.com
    Follow me on Twitter

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by abhijit.kamatkar View Post
    Partiularly, I am looking for the data based on description only "senior executive" and "HR". I do not wish to take any other records.
    the concept that seems to have escaped you is the WHERE clause
    Code:
    SELECT id
         , name
         , accountno
         , city
         , descrption
         , sex
      FROM employee
     WHERE descrption IN 
           ( 'senior executive'
           , 'HR' )
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •