Query issue selecting result from 1 table to another - mysql

I am not much of a code person, I am trying to run a query every 5 minutes in mssql.
Select user from Db1.dbo.tableA where requirement = 1
update Db2.dbo.tableB SET point = point + 5 where user = user;
I want to get user from Database 1 Table A where the requirement is 1 in DB, then update that user in Db2 Table B to match the user and increase current points by 5.
I am really newbie at this.

You can use UPDATE FROM with JOIN:
UPDATE b
SET b.point = b.point + 5
FROM tableB b
INNER JOIN tableA a
ON a.[user] = b.[user]
WHERE
a.requirement = 1

DECLARE #user <same datatype as in column>
Select #user = user from Db1.dbo.tableA where requirement = 1
update Db2.dbo.tableB SET point = point + 5 where user = #user;
Edit This will work only if there will be a single user as a result of the first query. For multiple users, the answer by Felix is more suitable

WITH tableAuser
AS
(
SELECT user FROM Db1.dbo.tableA WHERE requirement = 1
)
UPDATE b
SET b.point = b.point + 5
FROM Db2.dbo.tableB b
JOIN tableAuser a ON b.user = a.user

Related

Getting max revision number from a view is not working as I thought?

Using MySQL 5.6. I have a view that grabs from one main table called listquotes, and a few other items from other tables. The import columns in the table that are also in the view are Quote # and Revision. We might have 5 different revisions of the same quote number, so our table would look like
id .... Quote# Revision
================================
1 1234 1
2 1234 2
3 1234 3
4 1234 4
5 1234 5
Now on my application GUI we have a dropdown that should allow you to only see the most recent revisions of every quote. Here's how I try to do that
SELECT
...columns...
FROM view_allQuoteInfo
LEFT JOIN listcustomers c ON c.id = view_allQuoteInfo.customerId
WHERE 1=1 AND view_allquoteinfo.customerId = 2453 AND view_allquoteinfo.quoteStatusId = 2 AND view_allquoteinfo.Revision = (SELECT max(t.Revision) FROM view_allquoteinfo t WHERE view_allquoteinfo.`Quote #` = t.`Quote #`)
ORDER BY idx DESC
However, this is not giving me the results I want. Here's an analysis for tihs specific customer I did in excel, some quotes that only have one revision are not showing up like 6668 while others like 4730 which has two revisions is working properly
The where clause for the All table in excel is
1=1
AND view_allquoteinfo.customerId = 2453
AND view_allquoteinfo.quoteStatusId = 2
and the clause for the Latest Table is
1=1
AND view_allquoteinfo.customerId = 2453
AND view_allquoteinfo.quoteStatusId = 2
AND view_allquoteinfo.Revision =
(SELECT max(t.Revision)
FROM view_allquoteinfo t
WHERE view_allquoteinfo.`Quote #` = t.`Quote #`)
the only difference being me trying to get the mlatest revision. The logic looks right to me but obviously MySQL is not interpreting it the way I thought it would.
Any clue as to why it's not working or how I would fix it?
I would expect to see quote 6668 revision one and quote 5963 revision 4 in my "Latest" table, while quote 5963 revision 1,2,3 would not be there.
the condition WHERE view_allquoteinfo.Quote # = t.Quote # inside the subqiery is not clear ( t and view_allquoteinfo are the same table)
so try using a join on subquery group by quote
SELECT
...columns...
FROM view_allQuoteInfo
INNER JOIN (
SELECT v.`Quote #` my_quote, max(v.Revision) max_rev
FROM view_allquoteinfo v
GRUP BY v.`Quote #`
) t ON t.my_quote = view_allQuoteInfo.`Quote #`
AND t.max_rev = view_allQuoteInfo.Revision
LEFT JOIN listcustomers c ON c.id = view_allQuoteInfo.customerId
WHERE view_allquoteinfo.customerId = 2453
AND view_allquoteinfo.quoteStatusId = 2
ORDER BY idx DESC
I figured out a way that works for me. When the user wants all quotes, I leave it the way it is.
When the user wants only the latest revisions, I modify my select query to say
SELECT ...., MAX(view_allquoteinfo.Revision) as 'Revision',...
and then I add a
GROUP BY view_allquoteinfo.'Quote #'.

Get Records From Table 1, Comparing With Multiple Records From Table 2

I have 2 tables (users and usages)
USERS TABLE
username usage
a 32
b 5
c 5
USAGES TABLE
username usage_added
a 7
b 7
c 7
a 30
I want to get all items from USERS table, that have USAGE BIGGER than X (in this case, let's say X is 30) AND if either NO RECORDS are found with the same username in USAGES TABLE or if the usage_added for this username in USAGES TABLE are SMALLER than X (30 in our case)
So in this case, it should return no records. I have a codeigniter query
$this->db->select('users.username');
$this->db->from('users');
$this->db->join('usages', 'usages.username = users.username','left');
$this->db->where("(usages.email is NULL OR (usages.usage_added<30 AND usages.username=users.username))", NULL, FALSE);
$this->db->where("users.usage>30", NULL, FALSE);
By using above query, I still get "username a" returned.
Normally it should not return user A, because user a already has date 30 added. But it seems it compares to first record (a=7) and it says a<30 and it shows it again.
I hope it makes sense and somebody can help.
Written SQL Server syntax, this query should work for you:
DECLARE #usage_limit int = 30;
SELECT A.username
FROM users as A
LEFT OUTER JOIN
(
SELECT username,
usage_added = sum(usage_added)
FROM usages
GROUP BY
username
) as B
ON A.username = B.username
WHERE A.usage > #usage_limit
AND (B.username is null OR B.usage_added < #usage_limit)
This returns no records.
Hope this helps!
You seem to be describing logic like this:
select u.*
from users u
where u.usage > 30 or
not exists (select 1
from usages us
where us.username = u.username and
us.usage > 30
);
You should replace the 30 with a parameter if it varies.

Update max value from a mysql table

I'm trying to update a table's value using max(col) in a subquery, but for some reason, it's updating all values that match the user_id column I'm using.
This is my table structure:
user_id | steps | in_date
--------+-------+-----------
8 |10 | 1522246892
8 |10 | 1522250713
7 |10 | 1522250799
And this is my query:
UPDATE userdata
SET steps = (steps + 20)
WHERE user_id = 8
AND in_date = (SELECT max(in_date) WHERE user_id = 8);
I expected it to update only the second column, but it instead updates both columns with user_id = 8. Why isn't it working as expected? What am I doing wrong?
Edit: Thanks to Manoj's comment, I changed the query to the following, and it works:
UPDATE userdata
SET steps = (steps + 20)
WHERE user_id = 8
ORDER BY in_date DESC LIMIT 1;
Doing it his way is even better, since I don't have to run two queries, and already gets the highest one by id.
You will encounter sql error "You can't specify target table 'userdata' for update in FROM clause" when you use same table in the sub-query to update the same table.
Its strange that you say its running because,
- you missed from clause in your sub-query
- you can't use same table
Can you please be more specific.
seems you missed the from clause in subselect and for avoid conflit between select and updated value you could build a temp table using ainner subselect
UPDATE userdata
SET steps = (steps + 20)
WHERE user_id = 8
AND in_date = ( select max_date from (
SELECT max(in_date) max_date FROM userdata WHERE user_id = 8) t);
and in this way the AND clause could be always true

Select row if multiple present values are present in another table

I'm doing a search function on a movie database, I want to give the option to search a film with two genres (ie: crime id:6 and adventure id:7)
I basically want to get a row from title if it has genre_id 6 AND 7 present in the title_genre value. Obviously, this query below isn't working (I understand why it's not but I don't know how to fix it).
Any help please?
SELECT * FROM (`title`, `title_genre`)
WHERE `title`.`active` = 1
AND `title`.`media_id` = title_genre.media_id
AND title_genre.genre_id = 6 AND title_genre.genre_id = 7
You can use the exists to check the existence of other genre_id = 7 in title_genre and also using explicit join makes it much better as
select
t.*,
tg.*
from title t
join title_genre tg on tg.media_id = t.media_id
where
tg.genre_id = 6
and exists(
select 1 from title_genre tg1
where tg1.media_id = t.media_id
and tg1.genre_id = 7
)

Join and show latest Event

I need to generate a related of all "Tickets" that is open and with status "Analysis",i need to do a join in two tables.
The first one:
SELECT TOP 1000
[nsu_sugestao]
,[num_sugestao_papel]
,[edv_promotor]
FROM [NovoCLIC].[dbo].[Sugestao]
Exemplo of this select:
nsu_sugestao num_sugestao_papel edv_promotor
1 372759 92602045
In this one, num_sugestao_papel is the number of the ticket, edv_promotor is the person the owns it.
The second one:
SELECT TOP 1000
[iClic]
,[iStatus]
,[dtDateCreated]
FROM [NovoCLIC].[dbo].[T_STATUS_CLIC]
Exemple:
iClic iStatus dtDateCreated
1 1 1999-01-25 13:33:00.000
The iClic is the ticket number, the iStatus is the status currently and dtDateCreated.
I need to show all Tickets the the last status is analysis, the status analysis is the number 2 in iStatus column, i don't need the tickets that already are in different numbers than 2.
SELECT TOP 1000 [nsu_sugestao]
-- ,n.num_sugestao_papel
-- ,n.edv_promotor
-- ,s.iStatus
-- ,s.dtDateCreated
-- FROM [NovoCLIC].[dbo].[Sugestao]
-- with
-- select s.dtDateCreated,MAX(s.EventDate)latestDate
-- from [NovoCLIC].[dbo].[T_STATUS_CLIC] s
-- right join [NovoCLIC].[dbo].[T_STATUS_CLIC] s on [NovoCLIC].[dbo].[Sugestao] =
I'm trying something like this, but I'm getting a lot of errors.
Try the code given below and provide us feedback
SELECT
S.[nsu_sugestao]
,S.[num_sugestao_papel]
,S.[edv_promotor]
,T.[iClic]
,T.[iStatus]
,T.[dtDateCreated]
FROM [NovoCLIC].[dbo].[Sugestao] S
INNER JOIN [NovoCLIC].[dbo].[T_STATUS_CLIC] T on S.nsu_sugestao = T.iClic
INNER JOIN (
SELECT
[iClic]
,Max([dtDateCreated]) As LatestDateCreated
FROM
[NovoCLIC].[dbo].[T_STATUS_CLIC]
GROUP BY
[iClic]
) TM ON T.iClic = TM.iClic AND T.dtDateCreated = TM.LatestDateCreated
WHERE
T.iStatus = 2