PROBLEM
How do you add a column, with a default value, to an existing table in SQL Server 2000/2005?
ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
category
clientLastName
clientFirstName
incidentDescription
info field is not null then insert all fields into table 2
*need a unique id assigned
client_last_name
client_first_name
taskDescription
category
INSERT INTO Table2
(client_last_name, client_first_name, taskDescription, category)
(SELECT clientLastName, clientFirstName, incidentDescription, category
FROM Table1
WHERE info_field IS NOT NULL)
mysql> select id, user_id, name from personas_personas;
+----+---------+--------------+
| id | user_id | name |
+----+---------+--------------+
| 8 | 1 | startup |
| 9 | 1 | nerd |
| 10 | 1 | close |
| 12 | 2 | Nerd |
| 13 | 2 | Startup |
| 14 | 2 | Photographer |
+----+---------+--------------+
6 rows in set (0.00 sec)
mysql> select id, from_user_id, to_user_id, persona_id from friends_approvals;
+----+--------------+------------+------------+
| id | from_user_id | to_user_id | persona_id |
+----+--------------+------------+------------+
| 2 | 1 | 2 | 8 |
| 3 | 1 | 2 | 9 |
+----+--------------+------------+------------+
2 rows in set (0.00 sec)
from_user wants to approve to_user to a persona, then a record is inserted.to_user. If so, return is_approved=1 in the result set. Otherwise, return is_approved=0 in the result set. SELECT
pp.*,
CASE
WHEN exists (
SELECT
*
FROM
friends_approvals fa
WHERE
fa.from_user_id = pp.user_id AND
fa.persona_id = pp.id AND
fa.to_user_id = 2
)
THEN 1
ELSE 0
END as is_approved
FROM
personas_personas pp
WHERE
pp.user_id=1
SELECT
pp.*,
CASE
WHEN fa.from_user_id IS NOT NULL
THEN 1
ELSE 0
END as is_approved
FROM
personas_personas pp
LEFT OUTER JOIN friends_approvals fa ON
pp.user_id = fa.from_user_id AND
pp.id = fa.persona_id AND
fa.to_user_id = 2
WHERE
pp.user_id=1
questions and answered. answered contains the answers for all the questions for all the users. One user can answer a question multiple times. A question can be answered correctly or incorrectly.SELECT a.correct, count(*) as counter
FROM answered a
JOIN (SELECT user_id, question_id, max(created) as maxCreated
FROM answered
GROUP BY user_id, question_id) aux
ON a.user_id = aux.user_id AND
a.question_id = aux.question_id AND
a.created = aux.maxCreated
JOIN questions q ON a.question_id = q.id
WHERE a.user_id = 1 AND q.category_id = 1
GROUP BY a.correct
aux sub-query to select only the rows with the last answer to a question from a given user.Users table
id | name
-------------
1 | s1
2 | s2
3 | s3
4 | s4
5 | s5
6 | s6
friends table
friendID | user_a | user_b
--------------------
1 | 1 | 2
2 | 3 | 1
3 | 4 | 2
4 | 1 | 3
SELECT DISTINCT c.name
FROM users a, friends b, users c
WHERE a.id=b.user_a
AND b.user_b=c.id
AND a.name='s1';
Result:
╔══════╗
║ NAME ║
╠══════╣
║ s2 ║
║ s3 ║
╚══════╝