Inner join Identity table with another table from different DB - sql-server

I want to get the UserName from identity and associate it to his Id as foreign key from another table. I know I have to do an inner join, but I don't really know how I can do this using Razor, hope someone can help me
Code:
var users = UserManager.Users.ToList();
ViewBag.Users = new SelectList(db.Users_Groups.Where(o => o.IdGroup == id), "IdUser", /*Username here */);
DB:

Related

Getting items from one table that don't have a corresponding foreign key in another table

Hi I am working on a react app that is on a ruby on rails server setup, that uses postgres.
I have two tables in the database, movies and genres. In the movie table, there is a field genre id. And in the genre table its ID is how it links to movie table's genre id. I am trying to find all genres that don't have a corresponding id listed in the movie table. I have tried two different ways, with SQL directly.
sql = "SELECT count(*) FROM genres
LEFT JOIN movies
ON genres.id = movies.genre_id
WHERE genre.id = movies.genre"
genres_with_no_movie = ActiveRecord::Base.connection.exec_query(sql).rows[0][0]
The ruby on rails way.
genres_with_no_movie = Genre.joins(:movies).count()
Both of these give me the number of connections. It gives me places where a movie does have a corresponding genre. I am trying to find genres that don't have a movie that connects. I tried WHERE NOT EXISTS in place of where and that did not work.
Any help would be greatly appreciated.
LEFT JOIN allows you to retrieve the rows from the table genres and which have no correspondance in movies, ie movies.genre IS NULL :
SELECT count(*)
FROM genres
LEFT JOIN movies
ON movies.genre_id = genres.id
WHERE movies.genre IS NULL
An alternative perhaps quicker, and (imho more direct) uses NOT EXISTS instead of join.
select count(*)
from genres g
where not exists
(select null
from movies m
where m.genre_id = g.id
);

SQL Query for multiple tables including 2 'many to many' table

query2 = "SELECT k.key_tag, k.key_avail
FROM keyinfo k
JOIN keyingroup kig ON k.key_id = kig.key_id
WHERE kig.group_id = (
SELECT uig.group_id
FROM useringroup uig
JOIN users u ON uig.user_id = u.user_id
WHERE u.user_csn = '" & GV.userCSN & "')"
keyingroup table
useringroup table
Other than the 2 tables shown in the images, there is a keyinfo table, a accessgroup table and a users table. The idea here is that the 2 images is made as a 'many to many' relation table. Where in the table in image 1 is to show that a key can be accessed by multiple access groups while in the second image users can have multiple access groups and as long as a key falls within that access group, the user has permission to draw/loan it. The SQL statement above is what I have right now but it is not returning any rows at all and I have no idea if the way I'm querying is even right. Can someone provide some pointers as to what could be wrong and what I should have done?

How to append a table to another where the data being appended is not already in the appended table?

I have two tables in Ms Access that I want to append namely tblMaster and tblNew. The problem is some of the data in tblNew is already in tblMaster. How can I append tblNew to tblMaster and exclude the data that is already in tblMaster?
Having a specific unique key field would most certainly be easiest - but you can still accomplish it by simply using a combination fields as the unique key (essentially creating your own).
You can then do the update by linking the new table to the old table, on the fields that you designate should make up the unique key.
Suppose you have an "Employees" table and an "Employees New" table. You want the employee's name and badge number to form the unique key. This would be the SQL to add in any records that do not exist in the old employee table.
INSERT INTO [Employees]
( FIELD1,
EMPLOYEENAME,
BADGENUMBER,
FIELD2,
FIELD3,
FIELD4 )
SELECT NEW.FIELD1,
NEW.EMPLOYEENAME,
NEW.BADGENUMBER,
NEW.FIELD2,
NEW.FIELD3,
NEW.FIELD4
FROM [Employees New] AS NEW
LEFT JOIN [Employees] AS OLD
ON (NEW.EMPLOYEENAME = OLD.EMPLOYEENAME) AND
(NEW.BADGENUMBER = OLD.BADGENUMBER)
WHERE (OLD.EMPLOYEENAME Is Null);
This works by linking the "Employees" table with the "Employees New" using the fields determined to make up a primary key. It limits the results to show only those records where the new employee is not in the old employee table already.
The next decision would be deciding if you want to update existing records in the Employee table with the values in the new table. If so, you'd use an approach like this.
UPDATE [Employees] AS OLD
INNER JOIN [Employees New] AS NEW
ON (OLD.BADGENUMBER = NEW.BADGENUMBER) AND
(OLD.EMPLOYEENAME = NEW.EMPLOYEENAME)
SET OLD.Field1 = NEW.Field1,
OLD.Field2 = NEW.Field2,
OLD.Field3 = NEW.Field3,
OLD.Field4 = NEW.Field4;
This works by joining both the "Employees" and "Employees New" table, showing only records in both tables where the unique key fields match. We then update all fields.
Hopefully that puts you in the right direction.

SaaS- Tenant Specific Lookup Data in Shared Database

I am developing multitenant SaaS based application and going with Shared Database for storing all the tenant records with help of TenantId column.
Now the problem is i have some list of lookup records that needs to be shared for all the tenants. For example list of games.
GamesTable
Id
GameName
Also have another table used for storing only tenant specific records
TenantGames
Id
TenantId
GameName
The basic need is i want to use both table data and get the necessary details (Game_Name) while joining with another transaction table like UserGames. How can i achive this with this design? Here Game_Name can be either referred from Games Shared table or TenantSpecificGames table
Is there any other DB design which allows me to do mix both common master data and tenant master data with JOIN?
Basic requirement is keep common data and allow customization for the tenants if they want to add any new items.
This is the design I would then use.
Games
Id
GameName
IsTenantSpecific
SomeGameSpecificColumn
TenantGames
GameId
TenantId
SomeTenantSpecificColumn
AnotherTenantSpecificColumn
Then you can query that table in a Join with:
...
FROM
Games
INNER JOIN UserGames ON
UserGames.GameId = Games.Id
LEFT JOIN TenantGames ON
TenantGames.GameId = Games.Id
WHERE
TenantGames.TenantId = #tenantId OR
(
TenantGames.TenantId IS NULL AND
IsTenantSpecific = 0
)
Game specific fields can be put in the Games table. Tenant specific fields can be added to the TenantGames table, and those fields will be NULL if it is not a tenant specific customization.
We have a saas based database and we keep common data and tenant data in the same table.
Concept
GamesTable
Id NOT NULL
TenantId NULL
GameName NOT NULL
Add a unique key for TenantId and GameName
if TenantId is NULL you know it is common data
if TenantId is NOT NULL you know it belongs to a specific tenant and who exactly.
"Is there any other DB design which allows me to do mix both common
master data and tenant master data with JOIN?"
Yes
SELECT *
FROM GamesTable where TenantId = 'your tenant id'
UNION
SELECT *
FROM GamesTable where TenantId IS NULL -- common
This is a classic example of "many to many".
Table: Games
------------
GameID
GameName
IsMasterGame
TennantGames
------------------
GameID
TennantID
Tennants
------------
TennantID
...
To get the games for a given tennant, you would run a query like:
select *
from Games
where isMasterGame = true
union
select *
from Games g,
TennantGames tg
where g.GameID = tg.GameID
and isMasterGame = false
and tg.TennantID = $currentTennant
(Apologies for archaic join syntax)
The union allows you to ask two questions: which games apply to everyone (isMasterGame = true), and secondly which games apply to the current tennant (tg.TennantID = $currentTennant). Logically, tennant games cannot also be master games.
You can merge the tables leaving TenantId as NULL for records you wish to not be Tenant specific.
Games
Id
TenantId
GameName
The you can query that table in a Join with:
...
FROM
Games
INNER JOIN UserGames ON
UserGames.GameId = Games.Id
WHERE
Games.TenantId = #tenantId OR
Games.TenantId IS NULL
This will save you the trouble of ensuring that the Id is unique between the tables, unless you are using a UNIQUEIDENTIFIER for the Id.

Duplicate Data over One-to-Many self relation (Tsql)

Sorry if the title is poorly descriptive, but I can't do better right now =(
So, I have this master-detail scheme, with the detail being a tree structure (one to many self relation) with n levels (on SQLServer 2005)
I need to copy a detail structure from one master to the another using a stored procedure, by passing the source master id and the target master id as parameters (the target is new, so it doesn't has details).
I'm having troubles, and asking for your kind help in finding a way to keep track of parent id's and inserting the children without using cursors or nasty things like that...
This is a sample model, of course, and what I'm trying to do is to copy the detail structure from one master to other. In fact, I'm creating a new master using an existing one as template.
If I understand the problem, this might be what you want:
INSERT dbo.Master VALUES (#NewMaster_ID, #NewDescription)
INSERT dbo.Detail (parent_id, master_id, [name])
SELECT detail_ID, #NewMaster_ID, [name]
FROM dbo.Detail
WHERE master_id = #OldMaster_ID
UPDATE NewChild
SET parent_id = NewParent.detail_id
FROM dbo.Detail NewChild
JOIN dbo.Detail OldChild
ON NewChild.parent_id = OldChild.detail_id
JOIN dbo.Detail NewParent
ON NewParent.parent_id = OldChild.parent_ID
WHERE NewChild.master_id = #NewMaster_ID
AND NewParent.master_id = #NewMaster_ID
AND OldChild.master_id = #OldMaster_ID
The trick is to use the old detail_id as the new parent_id in the initial insert. Then join back to the old set of rows using this relationship, and update the new parent_id values.
I assumed that detail_id is an IDENTITY value. If you assign them yourself, you'll need to provide details, but there's a similar solution.
you'll have to provide create table and insert into statements for little sample data.
and expected results based on this sample data.

Resources