Selecting DISTINCT where not null in MySQL - mysql

I've got a table that has, for example, ID and Name.
There are multiple rows that contain blank names, but there also can be multiple rows with the same name. I'd like to select all IDs, counting each name only once (DISTINCT), but selecting ALL of the blanks.
The following query naturally only selects ONE blank name. How can I select all the distinct names plus all of the blank names?
SELECT DISTINCT Name, ID
FROM TestTable

SELECT DISTINCT Name, ID FROM TestTable where Name <> ''
union all
SELECT Name, ID FROM TestTable where Name = ''

Only slight improvement I can think of would be:
SELECT DISTINCT Name, ID FROM TestTable where Name <> ''
union all
SELECT '', ID FROM TestTable where Name IS NULL OR Name = ''

Related

MySQL query delivers wrong result when using <> (not equal) in WHERE clause

I came accross a strange problem with a MySQL Query
SELECT COUNT(id) FROM members
100
SELECT COUNT(id) FROM members WHERE lastname = 'Smith'
20
SELECT COUNT(id) FROM members WHERE lastname <> 'Smith'
0
The problem is, that the last query (Members with lastname != 'Smith') returns 0.
If there are 100 members in total and 20 members named 'Smith', the number of member with other last names should be 80, shouldn't it?
I tried different version using <>, !=, enclosing Smith with ' or ". The result when using LIKE and NOT LIKE instead is the same.
How is this possible? It seems that I am missing something quite obvious, but what...?
because others are null
try this :
SELECT COUNT(id) FROM members WHERE IFNULL(lastname ,'--')<> 'Smith'
Example :
CREATE TABLE my_table
SELECT 'ersin' name FROM dual
union all
SELECT 'ersin' name FROM dual
union all
SELECT 'ersin' name FROM dual
union all
SELECT null name FROM dual
union all
SELECT null name FROM dual
union all
SELECT null name FROM dual;
select script:
select count(*) from my_table where IFNULL(name ,'--') <> 'ersin' ;
output:
count(*)
3

I want to merge two tables [Tabl1] and [Tabl2] and show result by ID where [Tabl1].[names] = 'any string name'?

In my example where name like '' show all value tabl2 with tabl1
SELECT *
FROM
(SELECT
ID, names, NULL AS address, work, note
FROM
Tabl1
UNION
SELECT
ID, name, address, NULL, NULL
FROM
Tabl2) as x
ORDER BY
id, note DESC, address
With CTE_NAME(ID, names) --Column names for Temporary table
AS
(
SELECT ID , NAME FROM TABLE1
UNION
SELECT ID , NAME FROM TABLE2
)
SELECT * FROM CTE_NAME --SELECT or USE CTE temporary Table
WHERE name = "x"
ORDER BY ID
You'll need to use UNION to combine the results of two queries. In your case:
SELECT ID, names, NULL AS address, work, note
FROM Tabl1
GROUP BY names
UNION ALL
SELECT ID, name, address, NULL, NULL
FROM Tabl2
GROUP BY Tabl3
Note - If you use UNION ALL as in above, it's no slower than running the two queries separately as it does no duplicate-checking.

In mysql:how to display all column with distinct for one column

I want to display the column3 = 'ffff-jhj-01' with distinct values from column1.
Select distinct(name),phone,phone_id
FROM `calldetails`
where phone_id='ffff-jhj-01'
I have tried above query but it displays only column3 = 'ffff-jhj-01' not distinct of name.
If you want all columns with the distinct clause then try this query
Select distinct(name),phone,phone_id FROM `calldetails`
where
phone_id='ffff-jhj-01'
GROUP BY name
Group by will give you distinct first column, but remember you will not get distinct data for column two.
For this case you don't even need the distinct clause you can get same output with Group by alone, like
Select name,phone,phone_id FROM `calldetails`
where phone_id='ffff-jhj-01'
Group by name
output of both queries will be as follows
whereas if you go without Group by,
Select distinct(name),phone,phone_id FROM `calldetails`
where
phone_id='ffff-jhj-01'
then output will be as follows
Moreover you can get all the variants details of phone,i.e. column 2 with the following query
SELECT name, GROUP_CONCAT( phone ) , phone_id
FROM `calldetails`
WHERE phone_id = 'ffff-jhj-01'
GROUP BY name
LIMIT 0 , 30
But the phone field should be a text/varchar field
Try with:
Select distinct(name),phone,phone_id FROM `calldetails` where phone_id='ffff-jhj-01'

Get count of ID for name-value pair when name doesn't exist

In mysql - table like this:
ID Name Value
1 Color Blue
1 Weight 50
1 Type Fruit
2 Color Red
2 Weight 40
3 Color Yellow
I want to get a count of distinct ID's that have a name/characteristic of 'Type' and a distinct count of ID's that don't. The first one (those that do) is easy since it's defined, but if I do a select count(distinct ID) where name <> 'type' - ID 1 will still be part of that count as it has other rows/attributes that <> 'type'.
In this example - the desired result for distinct count = 'type' would be 1 (ID 1) and for distinct count <> 'type' would be 2 (ID's 2 & 3).
Thanks in advance.
This is similar to SQL Query User has one record in the table but not another one
You can select where the id is not in a subquery looking for ids that have type
select count(id) from table where id not in (select id from table where name = 'type')
group by id
for this particular task you can use:
select count(distinct ID) from table
where ID in (select ID from table where name='type') --- this will give you count of IDs where type exists
select count(distinct ID) from table
where ID not in (select ID from table where name='type') -- this will give you count of IDs without type
SELECT id FROM test GROUP BY id HAVING
CONCAT(",",CONCAT(GROUP_CONCAT(name), ","))
NOT LIKE '%,Type,%'
will give you all ids without Type: http://sqlfiddle.com/#!2/30837/1
(Concat with , ensures, that you are not matching XType by accident.)
while
SELECT COUNT(id) AS count, GROUP_CONCAT(id) AS ids
FROM(SELECT id, count(name) as count FROM test
GROUP BY id HAVING CONCAT(",",CONCAT(GROUP_CONCAT(name), ","))
NOT LIKE '%,Type,%') as temp;
will give you the desired count: http://sqlfiddle.com/#!2/30837/9
SELECT CASE
WHEN Name='Type' THEN 'Type'
ELSE 'Non-Type'
END Name
,ID
,COUNT(ID)
FROM Stuff
GROUP BY
CASE
WHEN Name='Type' THEN 'Type'
ELSE 'Non-Type'
END
,ID
See SQLFiddle

SELECT WHERE IN - mySQL

let's say I have the following Table:
ID, Name
1, John
2, Jim
3, Steve
4, Tom
I run the following query
SELECT Id FROM Table WHERE NAME IN ('John', 'Jim', 'Bill');
I want to get something like:
ID
1
2
NULL or 0
Is it possible?
How about this?
SELECT Id FROM Table WHERE NAME IN ('John', 'Jim', 'Bill')
UNION
SELECT null;
Start by creating a subquery of names you're looking for, then left join the subquery to your table:
SELECT myTable.ID
FROM (
SELECT 'John' AS Name
UNION SELECT 'Jim'
UNION SELECT 'Bill'
) NameList
LEFT JOIN myTable ON NameList.Name = myTable.Name
This will return null for each name that isn't found. To return a zero instead, just start the query with SELECT COALESCE(myTable.ID, 0) instead of SELECT myTable.ID.
There's a SQL Fiddle here.
The question is a bit confusing. "IN" is a valid operator in SQL and it means a match with any of the values (see here ):
SELECT Id FROM Table WHERE NAME IN ('John', 'Jim', 'Bill');
Is the same as:
SELECT Id FROM Table WHERE NAME = 'John' OR NAME = 'Jim' OR NAME = 'Bill';
In your answer you seem to want the replies for each of the values, in order. This is accomplished by joining the results with UNION ALL (only UNION eliminates duplicates and can change the order):
SELECT max(Id) FROM Table WHERE NAME = 'John' UNION ALL
SELECT max(Id) FROM Table WHERE NAME = 'Jim' UNION ALL
SELECT max(Id) FROM Table WHERE NAME = 'Bill';
The above will return 1 Id (the max) if there are matches and NULL if there are none (e.g. for Bill). Note that in general you can have more than one row matching some of the names in your list, I used "max" to select one, you may be better of in keeping the loop on the values outside the query or in using the (ID, Name) table in a join with other tables in your database, instead of making the list of ID and then using it.