I currently have 2 tables, one being a table that stores votes, and the other being the members table. I want SQL to pull the username from the "votes" table (as this keeps track of who voted), then add +1 "credits" in the table "members". Along with this, it will have to change the spaces to underscore and remove capital letters if they are used. Does anyone have an idea on how to do this?
Mysql – Pulling a username from table one to update credits on table 2 under the same username
MySQLPHP
Related Question
- Mysql – Update table from calculated temporary table
- Mysql – Is it possible to enforce a Unique Constraint on a Dataset comprised of joins
- Mysql – select members with the longest running absence streak
- Mysql – How to fix thesql uppercase query script in php
- Mysql – ny benefit to this additional table
- Mysql – inserting actual username of a person who edited record into a table with trigger MySQL
- Mysql – Trying to return a complete column from one table, and a count from another table
Best Answer
Unless you only planning to hold one "Vote" (ever!), I'd suggest you're missing at least one table.
The Username does not belong in the Votes table.
A User can exist without ever casting a vote, and so needs to exist in its own table.
Try to avoid using anything that might change as a Key. Going around the database changing duplicated values all over the place is (a) Poor Design and (b) terrible in terms of performance.
You cannot query or join effectively if your data is "noisy". This is another good reason to use a Surrogate Key (user_id) "inside" the database. It eliminates all these Data Entry requirements (for this part of the job, anyway).
The User's name should be stored once and only once so that, when it needs changing, you change it in (one and only) one place.
Using the tables I describe above, you get a list of who voted on this particular vote like this:
(And, if you wanted those who haven't voted yet)