mysql select columns and sum from other table - mysql

I have the following tables:
A: id, name, url
B: Aid, number
number can be any int
(Aid is the id from table A)
I want to select the name, url from table A and the SUM(B.number)
So the results will be something like:
name, url, SUM(B.number)
name, url, SUM(B.number)
name, url, SUM(B.number)
[..]
I need to join them somehow? How do i construct that query?

SELECT name, url, (SELECT SUM(number) FROM B WHERE B.Aid = A.id) As total
FROM A

SELECT A.name, A.url, SUM(B.number)
FROM A
LEFT JOIN B ON A.id = B.Aid
GROUP BY A.name, A.url
Please test it before cause I might have made a mistake since I got no MySQL DB available at the moment.

Related

sql query to replace column with same values if another column has duplicate values

have a table like the below:
id Phone
A 123456789
B 123456789
C 123456789
D 989898987
E 225897744
If the phone number is the same I need the id to be same in first table there are three id's (A, B, C) for same number. I need this to be related either A or B or C
id Phone
A 123456789
A 123456789
A 123456789
D 989898987
E 225897744
Please help me am not much aware of this type of hard query because am a beginner . Thanks in advance
You can use window functions:
select min(id) over (partition by phone) as id, phone
from t;
Or dispense with duplicates:
select min(id) as id, phone
from t
group by phone;
I see no reason to store duplicates in a table, so I am not proposing an update solution.
Here is an update query for your purpose, that would work on any MySQL verion:
update mytable t
inner join (select phone, min(id) min_id from mytable group by phone) m
on t.phone = m.phone and t.id <> m.min_id
set t.id = m.min_id
This works by self-joining the table with an aggregate query that gets the minimum id per phone: for rows where the id differs from the min_id of the same phone, the query updates the id.
Perhaps you are looking for something along these lines
select t2.id, t1.phone
from your_table t1
inner join (select min(id) as id, phone
from your_table
group by phone) t2 on t2.phone=t1.phone

find duplicates in pairs in mysql

I want to know how can I find duplicate value in a table over two columns combined.
suppose my table has fields as id || name || father_name || region || dob
now how can I find results set such as:
.ie I want to find all rows where three columns are same.
select t1.*
from your_table t1
join
(
select name, father_name, region
from your_table
group by name, father_name, region
having count(*) >= 3
) t2 on t1.name = t2.name
and t1.father_name = t2.father_name
and t1.region = t2.region
If you are using MySql 8.0, you could make use of window function. Below query with such function returns exact output:
select id, name, fatherName, country from (
select id,
name,
fatherName,
country,
count(id) over (partition by name, fatherName, country) cnt
from Tbl
) `a` where cnt > 1;
Actually, i also need this type of feature many times, where i need to compare all columns with same value except auto incremented primary key id column.
So, in that case i always use group by keyword.
Example,
SELECT A.*
FROM YourTable A
INNER JOIN (SELECT name,city,state
FROM YourTable
GROUP BY name,city,state
HAVING COUNT(*) > 1) B
ON A.name = B.name AND A.city = B.city AND A.state = B.state
You can append the number of columns which you want to compare
Hope, This might help you in your case also.

mySQL self join elegantly

My question is a bit dummy, but SQL has never been my cup of tea.
I have a simple table with 3 columns: id, name, parent_id (referring to an id in the same table).
I just want to get the relevant results to display them in an HTML page:
ID / Name / Parent Name
I can query as follows:
select id, name, parent_id from fields
union
select a.id, a.name, b.name
from fields a, fields b
where b.parent_id = a.id;
and do some tests, but i can figure out that it exists some more elegant manner.
Thx.
This query is best achieved by using a LEFT JOIN. This way you can still return fields which do not have a parent_id, i.e. are NULL. You'll also want to select the parent_name using an alias (parent_name in the example). This will allow you to programatically refer to the result by it's name rather than the numerical column index ($row->parent_name vs $row[2] if PHP is your language of choice).
SELECT f1.id, f1.name, f2.name parent_name
FROM fields f1
LEFT JOIN fields f2 ON f2.id = f1.parent_id
http://sqlfiddle.com/#!2/a9632/1/0
SELECT A.id AS ID, A.name AS Name, B.name AS Parent
FROM fields A, fields B
WHERE A.parent_id = B.id

SQL Query, query to having single field name from different table

I have two table with some identical field. (please don't blame the design).
Below only for the example schema
Table A
id
name
phone
keys
Table B
id
name
keys
address
So, i want to query id, name from either table A or B which meet condition 'keys' on single query, with return Field just "ID" and "NAME" no matter it's from tableA or tableB
with simple query
SELECT a.id, a.name, b.id, b.name FROM TABELA as a, TABLEB as b WHERE a.keys = '1' or b.keys = '1'
It return duplicate id, name, id1, name1 to the result field.
Use UNION instead of CROSS JOIN:
SELECT a.id, a.name
FROM TABELA as a
WHERE a.keys = '1'
UNION
SELECT b.id, b.name
FROM TABLEB as b
WHERE b.keys = '1'
use union or union all. Union returns only distinct rows, union all returns all rows
see examples in manual manual on unions
SELECT a.id, a.name FROM TABELA as a WHERE a.keys = '1'
union
SELECT b.id, b.name FROM TABELb as b WHERE b.keys = '1'
You are not actually joining tables, but you just want to combine the result of two different queries. We have UNION SELECT for that:
SELECT id, name FROM tableA
WHERE keys = '1'
UNION SELECT id, name FROM tableB
WHERE keys= '1'
If you want to order the result, you can use above as a subquery.

need to show only the duplicate value in sql

i have table as
id----name----roll-----class
1----ram-------1-----2
2----shyam-----2-----3
3----ram-------1-----3
4----shyam-----2-----3
5----ram-------1-----2
6----hari------1-----5
i need to find the the duplicate row only that have common name, roll, class. so the expected result for me is.
id----name----roll-----class
1----ram-------1-------2
2----shyam-----2-------3
4----shyam-----2-------3
5----ram-------1-------2
i tried to get from the query below but here only one field is supported. i need all three field common. Please do help me in this. thanks
SELECT *
FROM table
WHERE tablefield IN (
SELECT tablefield
FROM table
GROUP BY tablefield
HAVING (COUNT(tablefield ) > 1)
)
You can use count() over().
select id, name, roll, class
from (select id, name, roll, class,
count(*) over(partition by name, roll, class) as c
from YourTable) as T
where c > 1
order by id
https://data.stackexchange.com/stackoverflow/query/63720/duplicates
this will retun only the duplicate entry one time:
select t.id, t.name, t.roll, t.class
from table t
inner join table t1
on t.id<t1.id
and t.name=t1.name
and t.roll = t1.roll
and t.class=t1.class
this will return what you require:
select distinct t.id, t.name, t.roll, t.class
from table t
inner join table t1
on t.name=t1.name
and t.roll = t1.roll
and t.class=t1.class
I'd suggest something like this
SELECT A.* FROM
Table A LEFT OUTER JOIN Table B
ON A.Id <> B.Id AND A.Name = B.Name AND A.Roll = B.Roll AND A.Class = B.Class
WHERE B.Id IS NOT NULL
Something like that should work (I did not test though):
select a1.*
from table a1, a2
where (a1.id != a2.id)
and (a1.name == a2.name)
and (a1.roll== a2.roll)
and (a1.class== a2.class);
It seems there are several proprosals here. If it is a query that you'll use in your code, beware of the cost of the queries. Try an 'explain' with your database.