I would like to do a specific query
select distinct name
from tablename
to remove duplicates. But this gives me only the names. From 'select distinct name from table' I would like all columns returned with one where condition:
select *
from tablename
where value= 1
I tried this:
select *
from tablename
where value = 1 and exists (select distinct name
from tablename)
Unfortunately it returns the same data as:
select *
from tablename
where value = 1
Which means that there is a fundamental flaw in my query.
Could someone help me with my query. Thank you in advance.
Can you try this select field1, field2, field3 from tablename group by field2
This query will give you the id where you have duplicates:
select distinct id
from table1
group by id
having count(id)>1
and you can put
having count(id)=1
if you want to know the ones that don have duplicates
Related
[Expected Result]
I Need this result from Single table dont know how to do this.
You can use union all to duplicate each and every row in the resultset:
select custname, city from mytable
union all select custname, city from mytable
An order by clause might be useful:
select custname, city from mytable
union all select custname, city from mytable
order by custname, city
One simple method is:
select t.*
from t
union all
select t.*
from t;
If you care about the ordering, then you need an order by clause, so add:
order by custname;
Add ORDER BY to the UNION. For example:
select * from t union all select * from t order by id
See running example at DB Fiddle.
i have table
i would like to select like this
how to get data count_attendance?
you can use subquery to do that:
select name, gol, count(attendance) as count_data,(Select count(attendance) from tablename n where n.attendance='yes' and n.name=tablename.name and n.gol=tablename.gol) as count_attendance from tablename group by name,gol
Just replace tablename with the name of your table.
How can I select which table each result is from, when I use a UNION command to search multiple tables?
For example, if there are results from both tables, how can I add a column that will say (or differentiate between) whether it is from tableA or tableB.
try this one, simply add a virtual column for the name of the table.
SELECT *
FROM
(
SELECT *, 'tableA' as tableName FROM tableA
UNION ALL
SELECT *, 'tableB' as tableName FROM tableB
UNION ALL
SELECT *, 'tableC' as tableName FROM tableC
) s
WHERE colName = 'hello'
I am trying to exclude rows in my table based on the id's in other tables.
I have 2 tables of which a "select * from" results in a set like (1,2,3)
I am trying to combine the results from these 2 subqueries into one, like:
(1,2,3) + (4,5) = (1,2,3,4,5)
So I can filter the big table with a "NOT IN (1,2,3,4,5)"
I have been looking at GROUP_CONCAT's, UNION and all other kinds, but I can't seem to find something that actually works.
Anyone have a idea?
select *
from Table3
where id not in (
select id from Table1 --your subquery that returns 1,2,3
union all
select id from Table2 --your subquery that returns 4,5
)
select * from mytable
where id not in (
select id from othertable
union
select id from othertable2
)
I have two tables a and b which has a field name in it.
I need to list the data from these two tables. I thought of using union but in the result list data from the first table appears and then followed by the second.
what i want is to order by the field name so the result should be a mixed up of two tables in the order of name that is order by name.
select slug, name, 1 as mt
from tablea
union
select slug, name, 0 as mt
from tableb
order
by name;
The above is working well for me. will there be any complications in the result of this?
Suppose your query is
SELECT field1 FROM TABLE1 WHERE 1
UNION SELECT field1 FROM TABLE2 WHERE 1
u can make it a subquery like this
SELECT * FROM (SELECT field1 FROM TABLE1 WHERE 1
UNION SELECT field1 FROM TABLE2 WHERE 1) AS `result` ORDER BY `result`.`field1`
Or, you could use a Join query such as:
SELECT tablea.firstname, tablea.middlename, tablea.lastname, tableb.phone
FROM tablea, tableb
WHERE tablea.ID = tableb.ID
Then, you could sort the result however you like.