I want to select distinct combination of user age (a column name) and user name (another column name) , but when I write distinct (user_age, user_name), there is syntax error. If anyone have ideas how to write distinct with multiple columns it will be great.
BTW, using MySQL Workbench/MySQL
thanks in advance,
Lin
You have to leave the parenthesis. Just write
SELECT distinct user_age, user_name FROM foo where bar;
;-)
There are 2 ways to achieve this
SELECT DISTINCT user_age, user_name FROM table WHERE some_condition;
OR
SELECT user_age, user_name FROM table WHERE some_condition GROUP BY user_age, user_name;
Hope this helps
Related
Is there a way to get a column name from table where all values in this column are the same!
Example! IF i would there would be a such a code it would return answer 'Works'
Table1
ID Name Works
1 Andre Yes
2 John Yes
3 Stewart Yes
I don't know if the columns of the table are known. If not, you could be able to get them by:
desc Table1;
or if you are using higher version of MySQL, you could use:
select column_name from information_schema.columns where table_schema='your_schema' and table_name='Table1';
Then you try the following statement with parameters #column being replaced with the column names retrieved from the above statement:
select count(*) from (select count(*) as c from Table1 as t group by t.#column) as sub;
If the result is 1, the column is what you want. The result means how many distinct values this column has.
I suppose you will have to use a kind of programming language or stored procedure. You are not likely being able to achieve that with one single SQL statement.
I'm not sure if it's possible to run from MySQL itself but you can do this from your scripting engine.
You can run a loop on each column and do a query:
SELECT
COUNT(DISTINCT column_name)
FROM table_name;
And see you get 1 record in the results
Below shows my data table name called "company"
Here is my SQL query
SELECT
name, COUNT(DISTINCT num) AS count
FROM
company
WHERE
(num LIKE '51%' OR num LIKE '65%' OR num LIKE '81%')
GROUP BY
name
After run this query it shows below result
But I need below result. Please help me to solve my problem. Thank you
I assume you want to have the distinct on the first 2 characters of the column.
You can use the function LEFT() for this:
Query
SELECT name , COUNT(DISTINCT LEFT(num, 2)) AS count
FROM new_table
WHERE ( num LIKE '51%' OR num LIKE '65%' OR num LIKE '81%')
GROUP BY name
I need MySQL select query like
select wt_id,wt_name from work_type where cat_id=1,2,5..;
Is it possible?
Use IN operator ex.
... where cat_id IN (1,2,5..)
You just have to use IN operator
SELECT wt_id, wt_name
FROM work_type
WHERE cat_id IN (1,2,5..);
You can use SQL IN operator.
SELECT wt_id, wt_name
FROM work_type
WHERE cat_id IN (1,2,5..);
If you have any question please comment below.
In my MySQL table I have a column named member_id. That column stores values like this:
1,2,3,4,5
I need to check that by using
SELECT *
FROM member
WHERE 5 IN member_id
It's not working well. Please help me write a SQL query that will find the appropriate results.
Use this query:
SELECT *
FROM mytable
WHERE FIND_IN_SET(5, member_id)
Quassnoi's method is preferred for MySQL, but for a portable cross-platform technique, you can do this:
select *
from member
where member_id = '5'
or member_id like '5,%'
or member_id like '%,5,%'
or member_id like '%,5'
This assumes there is no extra whitespace in your data.
SELECT * FROM member WHERE member_id LIKE '%5%'
I am looking for a way to run a simple SELECT statment. I have a table which has two columns: id and email.
I want to run a SELECT statment that won't return duplicate values. For example, take the following data:
1 example#hotmail.com
2 example12#hotmail.com
3 example#hotmail.com
4 example#hotmail.com
I want it to return only the following:
1 example#hotmail.com
2 example12#hotmail.com
...and skip the duplicate values.
SELECT MIN(id), email FROM some_table GROUP BY email
SELECT DISTINCT email FROM table
If you don't need ID use
SELECT DISTINCT email FROM `TABLE_NAME`
else If you need the First ID use
SELECT MIN(ID),email FROM `TABLE_NAME` GROUP BY email
There are several ways to accomplish this, one is to use the DISTINCT clause:
SELECT DISTINCT email FROM your_table;
another way is to summarize counts of the values:
SELECT COUNT (*), email from your_table GROUP BY email;
SELECT DISTINCT UNIQUE_FEILD_NAME FROM YOUR_TABLE_NAME