I have a table such as:
id name ref_id order data_obj
-- ---- ------ ----- --------
1 Sam 0 15 [binary data]
2 Jack 0 20 [binary data]
3 Sue 0 25 [binary data]
4 Sam2 1 - [no data]
5 Sue2 3 - [no data]
6 Sam3 1 - [no data]
The idea is that I have more columns other than data_obj which can be common, so I don't want to insert them again, just want to insert a reference id to the same data.
Is it possible to write a query and select this:
1 - Sam - binary data from id 1
4 - Sam2 - binary data from id 1
6 - Sam3 - binary data from id 1
2 - Jack - binary data from id 2
3 - Sue - binary data from id 3
5 - Sue2 - binary data from id 3
Please note that I'm ordering according to column named order and there's no actual data for this column for referenced rows.
SELECT t1.id, t1.name, t2.data_obj
FROM your_table t1
LEFT JOIN your_table t2 ON t1.ref_id = t2.id
ORDER BY t1.order
Other version, which doesn't return rows without ref
SELECT t1.id, t1.name, t2.data_obj
FROM your_table t1, your_table t2
WHERE t1.ref_id = t2.id
ORDER BY t1.order
Here's a modification of #vartec's answer. This modification uses COALESCE() to combine the data_obj from either the primary row or the referenced row.
SELECT t1.id, t1.name, COALESCE(t1.data_obj, t2.data_obj)
FROM your_table t1
LEFT JOIN your_table t2 ON t1.ref_id = t2.id
ORDER BY COALESCE(t1.order, t2.order), ref_id;
COALESCE() is a standard SQL function that returns its first non-NULL argument.
Why aren't you using more than one table?
CREATE TABLE user (
user_id number not null (some form of auto increment or sequence),
name varchar(50) not null,
otherdata type,
primary key (id));
CREATE TABLE common (
common_id number not null (autoinc),
user_id number not null,
commondata type,
primary key (common_id),
unique index (user_id, common_id));
SELECT u.name, u.otherdata, c.commondata
FROM user u, common c
WHERE u.user_id = c.user_id
TABLE user
user_id name otherdata
1 Sam abc
2 Jack def
3 Sue ghi
Table common
common_id user_id commondata
1 1 AAA
2 1 BBB
3 1 CCC
4 2 DDD
5 3 EEE
6 3 FFF
Output
name otherdata commondata
Sam abc AAA
Sam abc BBB
Sam abc CCC
Jack def DDD
Sue ghi EEE
Sue ghi FFF
Related
I would like to get a value returned (in this case a zero) from the query for types in the event that a count of Table.id is zero.
SELECT COUNT(Table1.id) AS AccountID, Table2.typeName AS Types
FROM Table1
LEFT JOIN Table3
ON Table1.productID = Table3.productID
LEFT JOIN Table2
ON Table3.categoryID = Table2.CategoryID
WHERE Table3.account_id = 51
GROUP BY Table2.typeName;
This is the result that I would like to get
This is the result that I am currently getting
Table1
id ProductID
1 1
1 1
2 2
2 1
1 1
id in this table 1 references categoryID in table2
---------------------------------------------------------
Table2
categoryID typeName
1 SUV
2 BMW
3 Toyota
4 Audi
5 Suzuki
---------------------------------------------------------
Table3
categoryID ProductID account_id
2 1 51
1 2 52
3 1 51
categoryID is table3 references categoryID in table2
----------------------------------------------------------
My expected result should look like this
Count Types
0 Toyota
0 Audi
0 Suzuki
3 SUV
2 BMW
I want a count of all the id(s) from table1 with its corresponding typeName. If the id is not present, I want the a 0 to be returned with the corresponding typeName.
Following query will work:
SELECT
COUNT(Table1.id) AS AccountID,
Table2.typeName AS Types
FROM Table2
LEFT JOIN Table3
ON Table3.categoryID = Table2.CategoryID
AND Table3.account_id = 51
LEFT JOIN Table1
ON Table1.id = Table3.CategoryID
GROUP BY Table2.typeName;
Click here for DEMO
I have a table where user enters records (table1) :
table1
id name date desc amt
1 fred 11/30/2015 Bridge 123
2 fred 11/30/2015 Tunnel 234
I need to parse through table1 and create 3 records in table2 (or if the name/date/desc is already there, update the amt field) :
table2
id name date desc sortorder amt
3 fred 11/30/2015 Bridge 1 123
4 fred 11/30/2015 Bridge 2 123
5 fred 11/30/2015 Bridge 3 123
6 fred 11/30/2015 Tunnel 1 234
7 fred 11/30/2015 Tunnel 2 234
8 fred 11/30/2015 Tunnel 3 234
ID in each table is an AI key. Name and Date are indexed and foreign keys.
What is the most efficient way to do this?
Thanks!
You would insert the records as:
insert into table2(name, date, desc, sortorder amt)
select t1.name, t1.date, t1.desc, n.n, t1.amt
from table1 t1 cross join
(select 1 as n union all select 2 union all select 3) n
on duplicate key update amt = values(amt);
Note: for the on duplicate key update portion to work, you need a unique index on whatever defines a duplicate, perhaps:
create unique index unq_table1_4 on table1(name, date, desc, sortorder);
Table 1
NO_REG - ID
Table 2
NO_REG - BAGS
the problem its that table 1 contains lets say 3 ID, table 2 has the same NO_REG but just 1 row in BAGS
i do
SELECT BAGS, ID FROM TABLE 1 AS T1 INNER JOIN TABLE 2 AS T2 ON T1.NO_REG = T2.NO_REG
I get
NO_REG ID - BAGS
123 999- 2
123 989- 2
123 979- 2
I need
NO_REG ID - BAGS
123 999- 2
123 989- NULL (or 0)
123 979- NULL (or 0)
hope i was clear enough.
You need LEFT JOIN instead of INNER JOIN which retrieves matching values and null for non matching
Just something like
SELECT BAGS, ID FROM TABLE 1 AS T1 LEFT JOIN TABLE 2 AS T2 ON T1.NO_REG = T2.NO_REG
I have a record in table something like this
ID Name Value
1 abc 123
2 abc 152
3 cde 574
4 def 153
5 abc 777
How to delete row from the above column based on this algorithm,
We have same name for 3 rows (ID: 1,2,5). Delete all these rows except any one selected randomly. Same applied for other Names
Possible using sql or T-SQL?
;with d as (
select *, rowNum = row_number() over (partition by Name order by checksum(newid()))
from TableName
)
delete d
where rowNum > 1
Example table:
id foo bar
1 2 877
2 2 877
3 3 678
4 4 887
5 1 678
6 2 887
Example results:
id foo bar
3 3 678
4 4 887
5 1 678
6 2 887
The sql ignores the duplicates of 2 and 887 but not the latest one.
Foo will be specified beforehand.
How can this be achieved in MYSQL?
If I got you right you always want the bar value to a foo with the highest id.
select *
from your_table t1
where id = (select max(id) from your_table t2
where t2.foo = t1.foo)
Here is an SQL Fiddle for a demo. It also gives an alternative way with a left join. The construct does assume that the "latest one" is the one with the highest id.
EDIT: So according to your comment you only want that "highest id" for foo=2, that is a simply OR logic. I don't know if foo can be null so I added the IS NULL condition otherwise the <> might not work.
select * from your_table t1
where foo <> 2 or foo is null
or (foo = 2 and id = (select max(id) from your_table t2
where t2.foo = t1.foo) ) ;
Here is a demo