I have a table table_1 with a column parent_id with the value of the column id from other records from the same table.
I want to set another column out_degree with the number of records which have this table's id as their parent_id.
I tried this:
UPDATE table_1 p1
SET p1.out_degree = ( SELECT COUNT(*)
FROM table_1 p2,
table_1 p1
WHERE p2.parent_id = p1.id
GROUP_BY p1.id
)
But it didn't work. any idea?
Try this:
UPDATE table_1 T1
JOIN ( SELECT parent_id,Count(parent_id) as ParentCount
FROM table_1
GROUP BY parent_id
) T2 ON T1.parent_id=T2.parent_id
SET T1.out_degree=T2.parentCount
See the sample result in SQL Fiddle.
Related
I have a table_1 that looks something like this
NameId SelectedName
Null A
Null C
Null F
NameId is a FOREIGN KEY that REFERENCES table_2 (Id)
And a table_2 that looks like this
Id Name
1 A
2 B
3 C
4 D
5 E
6 F
I want to populate NameId with all of the Id numbers associated with the same Name, so for example, the final result would be:
NameId SelectedName
1 A
3 C
6 F
So far, all I have is:
SELECT Id, `Name` FROM table_2 WHERE `Name` IN (SELECT `SelectedName` FROM table_1);
Which I then tried to follow up with:
UPDATE table_1 SET NameId = Id WHERE SelectedName = `Name`;
Which doesn't work. Not sure how to go about doing this or how to use previously selected columns and relations to get what I want done here...
UPDATE table1
INNER JOIN table2
ON table1.SelectedName= table2.Name
SET table1.NameId = table2.ID
UPDATE Table1 a INNER JOIN Table2 b ON a.SelectedName = b.Name SET a.NameId = b.ID
First Update the table_1.NameId values with table_2.Id
UPDATE table_1 INNER JOIN table_2 ON table_1.SelectedName= table_2.Name
SET table_1.NameId= table_2.Id
and retrieve the values from table_1
SELECT NameId, SelectedName
FROM table_1
WHERE SelectedName IN (SELECT Name FROM table_2);
This way will update relation table column when parent has update.
DROP TRIGGER `table_2_au`;
DELIMITER ;;
CREATE TRIGGER `table_2_au` AFTER UPDATE ON `table_2` FOR EACH ROW
UPDATE `table_1` T1
INNER JOIN `table_2` T2
ON T1.`SelectedName` = T2.`Name`
SET T1.`NameId` = T2.`Id`;;
DELIMITER ;
I have a query in MySQL based on which I am finding duplicate records of some columns.
select max(id), count(*) as cnt
from table group by start_id, end_id, mysqltable
having cnt>1;
This above query gives me the max(id) and the count of number of records that have start_id,end_id,mysqltable column values same.
I want to delete all the records that match the max(id) column of the above query
How can I do that?
I have tried like below
delete from table
where (select max(id), count(*) as cnt
from table group by start_id,end_id,mysqltable
having cnt>1)
But Unable to delete records
You can remove duplicate records using JOIN.
DELETE t1 FROM table t1
INNER JOIN
table t2
WHERE
t1.id > t2.id AND t1.start_id = t2.start_id AND t1.end_id = t2.end_id AND t1.mysqltable = t2.mysqltable;
This query keeps the lowest id and remove the highest.
I think so this command should work:
delete from table
where id in
( select max(id) from table
group by start_id, end_id, mysqltable
having count(*) > 1
);
As I am running this query in MySql:
select count distinct(prim_key) IN table_1 inner join
table_2 on ID group by column name
I want to know total entries of a particular state for all the psu.
the table table_1 tells the total entries by the field agency.
Please help me.
select count(distinct prim_key)
from table_1 t1
inner join table_2 t2 on t1.ID = t2.ID
group by state
Try this::
select
count (distinct(prim_key))
from table_1
inner join table_2 on table1.ID=table_2.ID
group by column name
If I have three different tables like this
table_1
Field 1: victories
Field 2: name
table_2
Field 1: name
Field 2: birthday
Now I would want to get the birthday of the person with the most victories.
So I would do something like this (pseudo code):
select victories from table_1 and sum_it_all
get name and pass name to table_2
select birthday from table_2 where name
Ok, this is pretty ugly pseudo code, but I hope you get the point.
Using Andomar's solution works fine.
Now I tried to nest another table in it, like this though:
select address
from table_3
where birthday =
(
select birthday
from table_2
where name =
(
select name
from table_1
group by
name
order by
sum(victories) desc
limit 1
)
)
I do get a correct answer, but for some reason also get a null back. And how would I output the sum of victories?
select birthday
from table_2 t2
where name =
(
select name
from table_1 t1
order by
victories desc
limit 1
)
If one user can have multiple rows in table_1, you'd have to sum the victories:
select birthday
from table_2 t2
where name =
(
select name
from table_1 t1
group by
name
order by
sum(victories) desc
limit 1
)
I think what you're looking for is something like this:
SELECT t2.name, SUM(t1.victories) as SumOfVictories, t2.birthday
FROM table_1 as t1
JOIN table_2 as t2
ON table_1.name = table_2.name
GROUP BY t2.name, t2.birthday
ORDER BY SUM(t1.victories) DESC
LIMIT 1
You can use following nested SQL:
select name, birthday from table_2 where name in (
select name from table_1 order by victories desc limit 1
)
Would SELECT * FROM table_1 INNER JOIN table_2 ON table_1.name=table_2.name ORDER BY victories DESC give you the desired results?
This might be a solution for your problem:
SELECT table_1.name, table_2.birthday
FROM table_2
JOIN table_1 ON table_1.name=table_2.name
WHERE table_1.victories>=ALL(SELECT table_1.victories
FROM table_1)
try this:
Select name, birthday from table_2 t2
Join table_1 t1 On t1.Name = t2.name
Having Count(*) =
(Select Max(namCount)
From (Select Count(*) namCount
From table_1
Group By name))
Group By t1.name
Is it possible to get 1 result where I require data from 3 tables.
First table: I will need to grab all the fields (1 row found by a primary key)
Second table: I will need to grab the field 'username' (connected to first table by 'master_id')
Third table: I will need to grab the latest added row with the associated master_id key (table has 'date', 'master_id', 'previous_name').
select top 1 first.*, second.username, third.*
from first
inner join second on first.id = second.master_id
inner join third on first.id = third.master_id
order by
third.date desc
As always there are dozens of ways to skin a cat, I'm not sure if this is optimized as the subquery methods, but it should work.
You can join the three tables together. Then, you can use a "filter" join to keep only the latest Table3 row:
select *
from Table1 t1
join Table2 t2
on t2.master_id = t1.master_id
join Table3 t3
on t3.master_id = t1.master_id
join (
select master_id
, max(date) as max_date
from Table3
group by
master_id
) as filter
on t3.master_id = filter.master_id
and t3.date = filter.max_date
You'll need a correlated subquery for that third table.
SELECT t1.*, username, date, previous_name
FROM FirstTable t1
INNER JOIN SecondTable t2 ON t1.master_id=t2.master_id
INNER JOIN
(SELECT master_id, date, previous_name
FROM ThirdTable AS t3_1
WHERE date = (
SELECT MAX(date)
FROM ThirdTable AS t3_2
WHERE t3_2.master_id=t3_1.master_id)) q1 ON q1.master_id=t1.master_id;
NOTE: Untested.