We have two table with this structures:
table_user : id,name
table_user_groups: user_id, group_id
some users has no group so they have no entry in table_user_groups.
I want to determine these users and insert a default group_id for them.
I think I have to use an INSERT query with a JOIN condition.
any suggestion.
thanks in advance.
The Select statement to get the users you want is that and below that is the cursor for the insert.
SELECT A.id FROM table_user A
LEFT OUTER JOIN table_user_groups B ON A.id = B.user_id
WHERE B.user_id = NULL
DECLARE #userid int
DECLARE cursor CURSOR FOR
SELECT A.id FROM table_user A
LEFT OUTER JOIN table_user_groups B ON A.id = B.user_id
WHERE B.user_id = NULL
OPEN cursor
FETCH NEXT FROM cursor
INTO #userid
WHILE ##FETCH_STATUS = 0
BEGIN
Insert into table_user_groups(user_id,group_id) VALUES (userid, 'DEFAULTUSERID')
END
CLOSE vendor_cursor;
DEALLOCATE vendor_cursor;
Try something like this:
> INSERT INTO table_user_groups(user_id, group_id)
SELECT tu.id, [DEFAULT ID]
FROM table_user tu
LEFT JOIN table_user_groups tug ON tug.user_id = tu.id
WHERE tug.id IS NULL;
Where [DEFAULT ID] is the value what you want to use as default for this users.
UNTESTED
use this :
INSERT INTO table_user_group
(SELECT id,'yourdefaultID' FROM table_user WHERE id
NOT IN (SELECT user_id FROM table_user_groups))
Related
Can't find the same questions as mine I'm hoping you guys can help me.
I have this query for my SP what I need to do is check if the company Id is the same based on the parameter given by the user.
I have two parameters which are paramOrgA and paramOrgB which will be supplied by the user.
What I did was repeat the same query and set the returned value in two variables which is
orgAId and orgBId then use the IF statement to compare the value.
is there another way to accomplish this or should I say optimized way to do this?
Below is the query I wrote.
BEGIN
declare orgAId int;
declare orgBId int;
declare orgStatus bool;
SET orgAId = (SELECT c.Id
FROM Members as `m`
INNER JOIN Company as `c`
ON m.CompanyId = c.Id
WHERE m.Id = paramOrgA);
SET orgBId = (SELECT c.Id
FROM Members as `m`
INNER JOIN Company as `c`
ON m.CompanyId = c.Id
WHERE m.Id = paramOrgB);
IF (orgAId = orgBId) THEN
set orgStatus = true;
ELSE
set orgStatus = false;
END IF;
select orgStatus as 'CompanyStatus';
END IF;
END
One query will do
SELECT count(distinct c.Id) > 1 as 'CompanyStatus'
FROM Members as `m`
INNER JOIN Company as `c` ON m.CompanyId = c.Id
WHERE m.Id IN (paramOrgA, paramOrgB)
Even fewer keystrokes:
SELECT COUNT(DISTINCT CompanyId) > 1 as 'CompanyStatus'
FROM Members
WHERE Id IN (paramOrgA, paramOrgB)
I had an error in mysql as picture. please help me
sql code is following:
UPDATE
tbl_users AS Users
SET
Users.money_current = Users.money_current +
CASE
WHEN TempTbl.money_info IS NULL
THEN 0
ELSE TempTbl.money_info
END
LEFT JOIN
(SELECT
userId,
SUM(bet_money * bet_rate) AS money_info
FROM
tbl_betting
WHERE ROUND = 'xxx'
AND is_win = 1
GROUP BY userId) AS TempTbl
ON Users.userId = TempTbl.userId
FROM tbl_users AS Users;
This is the correct syntax:
UPDATE tbl_users AS Users
LEFT JOIN (
SELECT userId, SUM(bet_money * bet_rate) AS money_info
FROM tbl_betting WHERE ROUND = '965802' AND is_win = 1
GROUP BY userId
) AS TempTbl ON Users.userId = TempTbl.userId
SET Users.money_current = Users.money_current + COALESCE(TempTbl.money_info, 0)
I also changed that CASE expression with COALESCE().
But I think an INNER JOIN would also work in your case, since the unmatched rows of the LEFT JOIN that you use do not change the value of money_current.
You can see what I exactly mean on the image below.
I want to create new column "Bill To This Company" and get the company name from BillToCustomerID
Could be you need a self join
select a.customerId, a.CustomerName, a.BillToCustomerId, b.CustmerName
from my_table a
inner join my_table b on a.BillToCustomerId = b.customerId
Just self-join on the table and give that CustomerName an alias
select
c.CustomerId,
c.CustomerName,
c.BillToCustomerId,
b.CustomerName as "Bill To This Company"
from Customer c
left join Customer b on (b.CustomerId = c.BillToCustomerId)
Seems you need subquery :
select c.*,
(select c1.CustomerName
from Customer c1
where c1.BillToCustomerId = c.BillToCustomerId
order by c1.customerid
limit 1
) as BilltoThisCompany
from Customer c;
update tableName as t1 join tableName as t2 on t1.BillToCustomerID=t2.CustomerId
set t1.`Bill To This Company` = t2.customerName;
I hope someone can help. I have created this stored procedure to pull some infromation from different tables and when i tun it I get many duplicates, I was wondering if there was a way of sorting it?
use ICA
if exists(
select *
from INFORMATION_SCHEMA.ROUTINES
where SPECIFIC_SCHEMA = N'ICASchema'
and SPECIFIC_NAME = N'candidateOffer'
)
drop procedure ICASchema.candidateOffer
go
create procedure ICASchema.candidateOffer #candidateID INT
as
begin
select c.CandidateID,c.CandidateName, ca.CourseID, co.CourseName, o.OfferID, ot.OfferTypePoints
from [ICASchema].[tblCandidate] c
join [ICASchema].[tblCandidateOffer] o on c.CandidateID = o.CandidateID
join [ICASchema].[tblOffer] ot on o.offerID = ot.OfferType
join [ICASchema].[tblCandidateApplication] ca on ca.CourseID = ca.CourseID
join [ICASchema].[tblCourse] co on co.CourseName = co.CourseName
where c.CandidateID = #candidateID
--ORDER BY co.CourseName
GROUP BY co.CourseName, c.CandidateID,c.CandidateName, ca.CourseID, o.OfferID, ot.OfferTypePoints
end
go
Thanks
Graham
Following example is for creating a simple Proc_GetAllUsers stored procedure. This will solve your duplication issue.
DROP PROCEDURE IF EXISTS Proc_GetAllUsers
GO
CREATE PROCEDURE Proc_GetAllUsers( )
BEGIN
-- your sql queries
select * FROM users;
END
Go
try this:
select * from (select distinct c.CandidateID,c.CandidateName, ca.CourseID, co.CourseName, o.OfferID, ot.OfferTypePoints
from [ICASchema].[tblCandidate] c
join [ICASchema].[tblCandidateOffer] o on c.CandidateID = o.CandidateID
join [ICASchema].[tblOffer] ot on o.offerID = ot.OfferType
join [ICASchema].[tblCandidateApplication] ca on ca.CourseID = ca.CourseID
join [ICASchema].[tblCourse] co on co.CourseName = co.CourseName
where c.CandidateID = #candidateID
GROUP BY co.CourseName, c.CandidateID,c.CandidateName, ca.CourseID, o.OfferID, ot.OfferTypePoints) tablealias order by CourseName
This was the working solution.
[code]SELECT c.CandidateID, c.CandidateName, ICASchema.tblOffer.OfferTypePoints, ICASchema.tblCourse.CourseName
FROM ICASchema.tblCandidate AS c INNER JOIN
ICASchema.tblCandidateOffer ON c.CandidateID = ICASchema.tblCandidateOffer.CandidateID INNER JOIN
ICASchema.tblOffer ON ICASchema.tblCandidateOffer.CandidateOfferID = ICASchema.tblOffer.OfferID INNER JOIN
ICASchema.tblCourse ON ICASchema.tblOffer.OfferID = ICASchema.tblCourse.OfferTypeID
WHERE (c.CandidateID = '3')
[/code]
I have this query here
SELECT a.FileTrackingTag, a.DataSetName FROM TempTable a
LEFT JOIN FinalTable b ON a.Title = b.Title
WHERE b.FileTrackingTag IS NULL
AND b.DataSetName IS NULL
And I want to update the Filetrackingtag and the datasetname in finaltable with the values in temptable only if they are null and having the same Title and SampleID between the two tables
table structure is as such,
both tables have Title, Filetrackingtag, and Datasetname
Any recommendations?
I think the statement you're looking for is this:
UPDATE f SET
Filetrackingtag = isnull(f.Filetrackingtag, t.Filetrackingtag),
Datasetname = isnull(f.Datasetname, t.Datasetname)
FROM FinalTable f
JOIN TempTable t
ON f.Title = t.Title and f.SampleID = t.SampleID
(I haven't tested the code, but I'm pretty confident it should work)
Use derived table in UPDATE statement
UPDATE x
SET x.FileTrackingTag = x.newFileTrackingTag,
x.DataSetName = x.newDataSetName
FROM (
SELECT a.FileTrackingTag AS newFileTrackingTag, a.DataSetName AS newDataSetName,
b.FileTrackingTag, b.DataSetName
FROM TempTable a LEFT JOIN FinalTable b ON a.Title = b.Title AND a.SampleID = b.SampleID
WHERE b.FileTrackingTag IS NULL AND b.DataSetName IS NULL
) x