I would like to know if its possible to solve the following action with just one SQL-statement:
Table A: User-Table
Table B: Entries with foreign-keys for users
The relation between table A and Table B is 1 -> *
Because I don't want to count entries in table B for a specific user every time, I want to keep a column for this count in the user table. For that I would need a query that updates this column for each user.
Is this possible?
You can try this: UPDATE USERS U SET COUNT_ENTRIES=(SELECT COUNT(*) FROM ENTRIES WHERE USERID=U.ID)
Related
I am trying to delete a row in i.e. table 2 where the id is copied from table 1 query. I want to delete this row from table 2 and also from table 1 but I am having an issue where the command I have used all work but it does not seem delete from the tables. I believe this might be because of the relationship the tables have(I used mysql workbench to make the DB design)
I used this command :
delete
from doctorsTable
where Users_idUser in (select Users_idUser from Users where idUser = 20)
This is the relation :
As mentioned, I am trying to delete the row from doctorsTable with Users_idUser=20 and automatically it would delete from Users table idUser also with 20. I have tried the above command, it seems to run but its not really deleting the rows . please help !
I think what you want to do is to delete all rows with idUser from both tables with one statement.
You can do so by joining and deleting them like:
DELETE doctorsTable, Users
FROM doctorsTable
INNER JOIN Users ON doctorsTable.Users_idUser = Users.idUser
WHERE doctorsTable.Users_idUser = 20
In the DELETE line, you specify the table(s) from which to delete matching rows.
I have two tables:
Users
uid [pk]
Transactions
uid[fk]
amount
With a one to many relation between both of the tables, so now I want to add all the transactions for every user and show them in a table like this:
UID -> balance
Idon't really know how can I make do this for every user in my table...
Any ideas?
I don't want to store the results, I just want to show them on a table, and after that I'll export that table to Excel.
This SQL query will do the trick:
SELECT uid, SUM(amount) FROM Transactions GROUP BY uid
I have a web application that stores points in a table, and total points in the user table as below:
User Table
user_id | total_points
Points Table
id | date | user_id | points
Every time a user earns a point, the following steps occur:
1. Enter points value to points table
2. Calculate SUM of the points for that user
3. Update the user table with the new SUM of points (total_points)
The values in the user table might get out of sync with the sum in the points table, and I want to be able to recalculate the SUM of all points for every user once in a while (eg. once a month). I could write a PHP script that could loop through each user in the user table and find the sum for that user and update the total_points, but that would be a lot of SQL queries.
Is there a better(efficient) way of doing what I am trying to do?
Thanks...
A more efficient way to do this would be the following:
User Table
user_id
Points Table
id | date | user_id | points
Total Points View
user_id | total_points
A view is effectively a select statement disguised as a table. The select statement would be: SELECT "user_id", SUM("points") AS "total_points" FROM "Points Table" GROUP BY "user_id". To create a view, execute CREATE VIEW "Total Points View" AS <SELECT STATEMENT> where SELECT STATEMENT is the previous select statement.
Once the view has been created, you can treat it as you would any regular table.
P.S.: I don't know that the quotes are necessary unless your table names actually contain spaces, but it's been a while since I worked with MySQL, so I don't remember it's idiosyncrasies.
You have to user Triggers for this, to make the users total points in sync with the user_points table. Something like:
Create Trigger UpdateUserTotalPoints AFTER INSERT ON points
FOR EACH ROW Begin
UPDATE users u
INNER JOIN
(
SELECT user_id, SUM(points) totalPoints
FROM points
GROUP BY user_id
) p ON u.user_id = p.user_id
SET u.total_points = p.totalPoints;
END;
SQL Fiddle Demo
Note that: As noted by #FireLizzard, if these records in the second table, are frequently updated or delted, you have to have other AFTER UPDATE and AFTER DELETE triggers as well, to keep the two tables in sync. And in this case the solution that #FireLizzard will be better in this case.
If you want it once a month, you can’t deal with just MySQL. You have too « logic » code here, and put too logic in database is not the correct way to go. The trigger of Karan Punamiya could be nice, but it will update the user_table on every insert in points table, and it’s not what you seem to want.
For the fact you want to be able to remove points, just add bsarv new negated rows in points, don’t remove any row (it will break the history trace).
If you really want it periodically, you can run a cron script that does that, or even call your PHP script ;)
I would like to know the best way to check one table of data, and when no more rows exist matching the WHERE clause, delete a row from another table.
I have tried myself but it has become too cumbersome with 6 queries and nested if/else, and it doesn't work to top it off.
I have never used SQL join's before, so examples will help me to understand responses.
I have a table of devices, there is a master table with a device and a password.
There is a second table containing the multiple rows of the device in the above table, and a series of serial numbers.
When the second table no longer contains any of the serial numbers listed in the master table, I want the row containing the device and password from the master table.
If you mean like when you have a table customer and a table order, delete the customers if they have no orders? Then you can use subselect:
delete from customer where customerid not in (select customerid from order)
You coult make a DELETE statement like
DELETE FROM masterTable WHERE ID NOT IN (SELECT masterTableID FROM secondaryTable)
This would delete all the rows from the master-table which don't have any references in the second table. That also means it would not delete only one row, but all of the matching ones. The only necessary thing you need is that every row in the second table references to the master table.
DELETE table_devices
FROM table_devices
left JOIN serial ON table_devices.id= serial.device_id
WHERE serial.device_id is null
I have two tables one contains a list of teams, the other contains a list of schedules for these teams and score results. I want to be able to update the teams table when I insert a row into the schedules table. So for example if I have:
Teams table
Team1
Team2
.
Schedules table
team1, 3
team2, 1
what I want to be able to do is when the score fields are updated I need to update different fields in the teams table. So when I update this schedule row,
It would insert 3 into one of the rows for team1, and 1 for the team2
Also I would like to be able to calculate the difference between the scores and also insert this into the teams table.
What would be the best way to implement this?
And I would need a function in order to do the comparison of the scores right?
Thanks,
So far I thought of doing something like this.
update table teams set teams.gamesplayed = teams.gameplayed +1 /*this would add one to the games played field because they just played.*/
then for the goals scored something like
update teams set teams.gf =
(
select t.goalsscored
from schedule t, teams s
where t.teamname = s.team1name AND )
)
you need add ON UPDATE and ON INSERT triggers to Teams table - thats all.
In trigger you can do anything you need. Of course you can create standalone function and call it from triggers.