Results 1 to 4 of 4

Thread: SQL query to return unique output based two fields from one table

  1. #1
    Join Date
    Jan 2009
    Posts
    45

    SQL query to return unique output based two fields from one table

    Hi Guys,

    My problem is I want SQL query to return unique output based on fields from two tables.


    I have a table tag_phto which is a join table and has two fields: tag_Id and Phto_Id.

    I have to create a page which will have a list of all tags and a thumbnail against each tag in the table.But I only have to show each tag only once and a different photo for each tag.

    Can you able to help me to solve this issue?..

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: SQL query to return unique output based two fields from one table

    Hi ,

    Have you tried below method ? if not then try it.This method will definitely give you the desired output:

    Query:

    select tag_Id, Min(Phto_Id)
    from tags_phto
    group by tag_Id

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: SQL query to return unique output based two fields from one table

    The answer for your problem is very easy.I also wanted the similar type of output.
    At that time I used DISTINCT.The DISTINCT avoids the duplication in the output.

    Syntax:

    SELECT DISTINCT field1, field2..... from table_name

    Example:

    SELECT DISTINCT tag_id, phto_id from tag_phto

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: SQL query to return unique output based two fields from one table

    According to me this is because what you want is not achievable via SQL only. If I can understand your request correctly, what you are asking for is a page with the tag displays only once and all of the photos associated with that particular tag.

    See below:

    Tag PhtoTag
    6 26
    16
    78


    Your page will accept a Tag and independently get all of the phtoTag associated with that perticular tag .But SQL will give you a repeating value for Tag.

Similar Threads

  1. PHP mysql select unique records from one table
    By Captain Carrot in forum Software Development
    Replies: 6
    Last Post: 13-05-2010, 12:18 PM
  2. Select unique records from a table
    By Conraad in forum Software Development
    Replies: 5
    Last Post: 13-05-2010, 12:01 AM
  3. Need list of fields of table in print
    By Murder in forum Windows Software
    Replies: 4
    Last Post: 03-09-2009, 05:17 PM
  4. How to Create view based on table-join query
    By Preetish in forum Windows Software
    Replies: 3
    Last Post: 12-08-2009, 01:44 PM
  5. Combination of Two fields in one SQL Query
    By Andrzej in forum Software Development
    Replies: 3
    Last Post: 19-02-2009, 11:58 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,727,337,645.49088 seconds with 17 queries