Jump to content

select all with distinct


dreamwest

Recommended Posts

Im trying to select distinct title from a column but also need to select all columns

 

Heres what ive originally got, this has duplicates in title column:

 

$sql = "SELECT * from table where (title like '%{$search_id}%') ";

 

If i select distinct info from the other columns wont get selected:

$sql = "SELECT DISTINCT title from table where (title like '%{$search_id}%') ";

 

How can i select distince titles and also select all coumns with distinct titles in one query

Link to comment
Share on other sites

What exactly do you want to achieve?

$sql = "SELECT *, DISTINCT title from table where (title like '%{$search_id}%') ";

should work... although I'm not sure if this is what you want.

Link to comment
Share on other sites

DISTINCT is not a function that applies to the column following it. When it is present in the select term, it causes duplicate (with exactly the same values in everything that is selected) rows in the result set to be removed.

 

Could you post an example of the data you have and what results you want? Do you have multiple rows with the same title but with other unique values for the other columns and if so, which of the other unique values do you want to select?

Link to comment
Share on other sites

What exactly do you want to achieve?

$sql = "SELECT *, DISTINCT title from table where (title like '%{$search_id}%') ";

should work... although I'm not sure if this is what you want.

 

I get an error when i try this

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT title from table where (title like '%search icon%') order by title' at line 1

 

I need to select over 20 columns so i cant keep naming each one

 

SELECT DISTINCT title, colname1, colname2, etc
FROM table
WHERE title LIKE '%{$search_id}%'

 

 

Link to comment
Share on other sites

The following is the syntax prototype for a SELECT statement. Among other things, it specifies the order in which elements must be listed. DISTINCT must immediately follow the SELECT keyword -

 

SELECT

    [ALL | DISTINCT | DISTINCTROW ]

      [HIGH_PRIORITY]

      [sTRAIGHT_JOIN]

      [sql_SMALL_RESULT] [sql_BIG_RESULT] [sql_BUFFER_RESULT]

      [sql_CACHE | SQL_NO_CACHE] [sql_CALC_FOUND_ROWS]

    select_expr [, select_expr ...]

    [FROM table_references

    [WHERE where_condition]

    [GROUP BY {col_name | expr | position}

      [ASC | DESC], ... [WITH ROLLUP]]

    [HAVING where_condition]

    [ORDER BY {col_name | expr | position}

      [ASC | DESC], ...]

    [LIMIT {[offset,] row_count | row_count OFFSET offset}]

    [PROCEDURE procedure_name(argument_list)]

    [iNTO OUTFILE 'file_name' export_options

      | INTO DUMPFILE 'file_name'

      | INTO var_name [, var_name]]

    [FOR UPDATE | LOCK IN SHARE MODE]]

 

SELECT DISTINCT * is valid syntax.

 

Also -

The ALL, DISTINCT, and DISTINCTROW options specify whether duplicate rows should be returned. If none of these options are given, the default is ALL (all matching rows are returned). DISTINCT and DISTINCTROW are synonyms and specify removal of duplicate rows from the result set.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.