I have a table with a lot of columns and I want to select all columns but I want unique one of these columns.
This works for me but I don't get all columns:
$result = mysql_query("SELECT DISTINCT company FROM table t $order");
I also tested this but doesn't do anything:
$result = mysql_query("SELECT * DISTINCT company FROM table t $order");
$result = mysql_query("SELECT DISTINCT company * FROM table t $order");
EDIT
My table has a lot of columns let's say it has 5 so an example of my records is this:
company x y price numOfEmpl
one 1 5 1.3 15
one 2 6 1.4 15
two 3 7 1.5 16
three 4 8 1.6 17
So I want to cut the second line and take all the others.
Edited previous answer based on extra information
Have a solution for you in MySQL and in SQL Server if you need it
MySQL Example (Using User Variables/Sub-Queries)
CREATE TABLE SomeTable (company VARCHAR(20), x INT, y INT, price FLOAT, numOfEmploy INT);
INSERT INTO SomeTable
(company, x, y, price, numOfEmploy)
VALUES
('one', 1, 5, 1.3, 15),
('one', 1, 6, 1.4, 15),
('two', 1, 7, 1.5, 16),
('three', 1, 8, 1.6, 17);
SET @count = NULL, @value = NULL;
SELECT company, x, y, price, numOfEmploy FROM (
SELECT
company, x, y, price, numOfEmploy,
@count := IF(@value = company, @count + 1, 1) AS rc,
@value := company
FROM SomeTable
) AS grouped_companies WHERE rc = 1
SQL Server Example (Using CTE)
--Create the table
declare @sometable table ( company varchar(10), x int, y int, price float, numOfEmploy int)
--insert the data
insert into @sometable values ('one', 1, 5, 1.3, 15)
insert into @sometable values ('one', 2, 6, 1.4, 15)
insert into @sometable values ('two', 3, 7, 1.5, 16)
insert into @sometable values ('three', 4, 8, 1.6, 17)
--WITH Common Table Expression
;WITH grouped_companies AS (
SELECT *,
ROW_NUMBER() OVER(PARTITION BY company
ORDER BY company) AS rc
FROM @sometable)
SELECT gc.company, gc.x, gc.y, gc.price, gc.numOfEmploy
FROM grouped_companies gc
WHERE gc.rc = 1
The DISTINCT
keyword can be used to return only distinct (different) values within defined columns, like
SELECT DISTINCT column_name,column_name
or you can return the amount of DISTINCT values by
SELECT COUNT(DISTINCT column_name)
I think you might need to use a seperate sql query
for all the records