i have a 'results' table that has a datetime colum 'created_time'.
this table reference another table 'results_values' (results.id reference results_values.results_id). here are tables describe:
mysql> describe webforms_results;
+------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| webform_id | int(11) | NO | | NULL | |
| store_id | int(11) | NO | | NULL | |
| customer_id | int(11) | NO | | NULL | |
| customer_ip | bigint(20) | NO | | NULL | |
| created_time | datetime | NO | | NULL | |
| update_time | datetime | NO | | NULL | |
| approved | tinyint(1) | NO | | NULL | |
| view_on_frontend | tinyint(1) | NO | | 0 | |
+------------------+------------+------+-----+---------+----------------+
mysql> describe webforms_results_values;
+--------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| result_id | int(11) | NO | MUL | NULL | |
| field_id | int(11) | NO | | NULL | |
| value | text | NO | | NULL | |
| rating | int(11) | YES | | NULL | |
| created_time | datetime | YES | | NULL | |
+--------------+----------+------+-----+---------+----------------+
now i've added a datetime column 'created_time' on 'results_values', and i want to insert datetimes on the second tables copying them from the first table on the corresponding id. is there a quick way to do it?
yes triggers are the solution. And if you want to update already inserted data, you can do :
UPDATE webforms_results_values wrv INNER JOIN webforms_results wr
ON wrv.result_id = wr.id
SET wrv.created_time = wr.created_time
Create "Triggers" to update each and every update of those tables.
Please refer following URL:
Triggers
Related
I have 2 tables - patients, and issuers.
I with to extract entire patients table along with issuer_name from patients and issuers table. Optionally there might be no issuer of patient identifier.
If i do:
select * from patients, issuer_name where patients.issuer_of_patient_identifier=issuer.issuer_id doesn't return anything in case for the corresponding patient table row issuer_of_patient_identifier is NULL.
How do i accomplish this?
mysql> describe patients;
+------------------------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+------------------------------+--------------+------+-----+-------------------+-----------------------------+
| patient_id | int(11) | NO | PRI | NULL | auto_increment |
| patient_identifier | varchar(64) | YES | MUL | NULL | |
| issuer_of_patient_identifier | int(11) | YES | MUL | NULL | |
| medical_record_locator | varchar(64) | YES | | NULL | |
| patient_name | varchar(128) | NO | MUL | NULL | |
| birth_date | datetime | YES | | NULL | |
| deceased_date | datetime | YES | | NULL | |
| gender | varchar(16) | YES | | NULL | |
| ethnicity | varchar(45) | YES | | NULL | |
| date_created | datetime | NO | | CURRENT_TIMESTAMP | |
| last_update_date | datetime | YES | | NULL | on update CURRENT_TIMESTAMP |
| last_updated_by | varchar(128) | NO | | NULL | |
+------------------------------+--------------+------+-----+-------------------+-----------------------------+
12 rows in set (0.00 sec)
mysql> describe issuers;
+------------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+-------------------+-----------------------------+
| issuer_id | int(11) | NO | PRI | NULL | auto_increment |
| issuer_name | varchar(64) | NO | MUL | NULL | |
| issuer_uid | varchar(128) | YES | | NULL | |
| issuer_uid_type | varchar(64) | YES | | NULL | |
| date_created | datetime | NO | | CURRENT_TIMESTAMP | |
| last_update_date | datetime | YES | | NULL | on update CURRENT_TIMESTAMP |
| last_updated_by | varchar(128) | NO | | NULL | |
+------------------+--------------+------+-----+-------------------+-----------------------------+
The query is
select * from patients
LEFT JOIN issuer_name ON
patients.issuer_of_patient_identifier = issuer.issuer_id
Without joins:
select * from patients, issuer_name
where patients.issuer_of_patient_identifier = issuer.issuer_id
OR patients.issuer_of_patient_identifier IS NULL
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax. Avoid tutorials that use commas. Challenge instructors that show examples with them.
You want a LEFT JOIN:
select *
from patients p left join
issuer_name i
on p.issuer_of_patient_identifier = i.issuer_id;
I have the following 2 tables (student and attendance):
mysql> describe student;
+----------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+--------------+------+-----+---------+----------------+
| student_id | int(11) | NO | PRI | NULL | auto_increment |
| student_email | varchar(255) | YES | | NULL | |
| student_phone_number | varchar(255) | YES | | NULL | |
| parent_first_name | varchar(255) | NO | | NULL | |
| parent_last_name | varchar(255) | NO | | NULL | |
| parent_email | varchar(255) | NO | | NULL | |
| parent_phone_number | varchar(255) | NO | | NULL | |
| first_name | varchar(255) | NO | | NULL | |
| last_name | varchar(255) | NO | | NULL | |
| days_absent | int(11) | YES | | NULL | |
| days_tardy | int(11) | YES | | NULL | |
| class_id | int(11) | NO | MUL | NULL | |
+----------------------+--------------+------+-----+---------+----------------+
mysql> describe attendance;
+-----------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------+------+-----+---------+-------+
| student_id | int(11) | NO | PRI | NULL | |
| class_id | int(11) | NO | PRI | NULL | |
| attendance_date | date | NO | PRI | NULL | |
| absent | tinyint(1) | YES | | NULL | |
| tardy | tinyint(1) | YES | | NULL | |
| note | text | YES | | NULL | |
+-----------------+------------+------+-----+---------+-------+
I want to check the attendance and email of all the children who are absent or tardy for the day. Is there a SQL statement I can use that can, for example, select the students from attendance with an absent/tardy value = 1, then using that student_id specified in the attendance table, pull all the student info from the student table where attendance.student_id = student.student_id?
just try exists
select student_email,student_phone_number from student std
where exists(
select 1 from attendance atd where std.student_id = atd.student_id
and (atd.absent =1 or atd.tardy =1)
)
just make sure you select all the info from student table and exists all the filter in attendance talbe. the condition is student_id.
Also if you need info from attendance talbe, then use join.
How can I select all rows from the 'trade' table that have a 'ticker_id' field equal to the 'id' field of a row from the 'ticker' table, that has a 'name' field equal to 'MSFT'?
The 'trade.ticker_id' field is a foreign key to the 'ticker.id' field.
Is this a bad design if I want filter a lot of 'trade' rows?
mysql> describe ticker;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(64) | NO | | NULL | |
| long_name | varchar(250) | YES | | NULL | |
| exchange_id | int(11) | YES | MUL | NULL | |
+-------------+--------------+------+-----+---------+----------------+
4 rows in set (0,00 sec)
mysql> describe trade;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| time | datetime | NO | | NULL | |
| price | float | NO | | NULL | |
| quantity | int(11) | NO | | NULL | |
| source | varchar(64) | YES | | NULL | |
| buyer | varchar(64) | YES | | NULL | |
| seller | varchar(64) | YES | | NULL | |
| initiator | varchar(64) | YES | | NULL | |
| ticker_id | int(11) | YES | MUL | NULL | |
+-----------+-------------+------+-----+---------+----------------+
9 rows in set (0,00 sec)
SELECT * FROM trade TR
LEFT JOIN ticker TI ON TR.ticker_id = TI.id
WHERE TI.name = "MSFT"
I have two databases.
One is called INFO with three tables (Stories, Comments, Replies)
Stories has the following fields
+--------------+----------------+------+-----+---------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------------+------+-----+---------------------+-----------------------------+
| storyID | int(11) | NO | PRI | NULL | |
| originalURL | varchar(500) | YES | | NULL | |
| originalDate | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| numDiggs | int(11) | YES | | NULL | |
| numComments | int(11) | YES | | NULL | |
| diggURL | varchar(500) | YES | | NULL | |
| rating | varchar(50) | YES | | NULL | |
| title | varchar(200) | YES | | NULL | |
| summary | varchar(10000) | YES | | NULL | |
| uploaderID | varchar(50) | YES | | NULL | |
| imageURL | varchar(500) | YES | | NULL | |
| category1 | varchar(50) | YES | | NULL | |
| category2 | varchar(50) | YES | | NULL | |
| uploadDate | timestamp | NO | | 0000-00-00 00:00:00 | |
| num | int(11) | YES | | NULL | |
+--------------+----------------+------+-----+---------------------+-----------------------------+
Another database is called Data with one table (User). Fields shown below:
+-------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+-------------+------+-----+---------+-------+
| userID | varchar(50) | NO | PRI | NULL | |
| numStories | int(11) | YES | | NULL | |
| numComments | int(11) | YES | | NULL | |
| numReplies | int(11) | YES | | NULL | |
| numStoryDiggs | int(11) | YES | | NULL | |
| numCommentReplies | int(11) | YES | | NULL | |
| numReplyDiggs | int(11) | YES | | NULL | |
| numStoryComments | int(11) | YES | | NULL | |
| numStoryReplies | int(11) | YES | | NULL | |
+-------------------+-------------+------+-----+---------+-------+
User.userID is full of thousands of unique names. All other fields are currently NULL. The names in User.userID correspond to the names in Stories.uploaderID.
I need to, for each userID in User, count the number of stories uploaded from (i.e. num) Stories for the corresponding name and insert this value into User.numStories.
The query which I have come up with (which produces an error) is:
INSERT INTO DATA.User(numStories)
SELECT count(num)
FROM INFO.Stories
WHERE INFO.Stories.uploaderID=DATA.User.userID;
The error I get when running this query is
Unknown column 'DATA.User.userID' in 'where clause'
Sorry if this is badly explained. I will try and re-explain if need be.
You aren't creating new entries in the User table, you're updating existing ones. Hence, insert isn't the right syntax here, but rather update:
UPDATE DATA.User u
JOIN (SELECT uploaderID, SUM(num) AS sumNum
FROM INFO.Stories
GROUP BY uploadedID) i ON i.uploaderID = u.userID
SET numStories = sumNum
EDIT:
Some clarification, as requested in the comments.
The inner query sums the num in Stories per uploaderId. The updates statement updates the numStories in User the the calculated sum of the inner query of the matching id.
I have following table called transactions and I want to add foreign key using column account_id. But when I execute the query (see below) 0 rows affected. I am not getting what is wrong with the query.
I have two tables called transactions and accounts. Accounts table has an id as primary key and an account has_many transactions.
transactions table
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| account_number | int(11) | YES | | NULL | |
| m_number | varchar(255) | YES | | NULL | |
| registration_number | varchar(255) | YES | | NULL | |
| page_number | varchar(255) | YES | | NULL | |
| entry_date | datetime | YES | | NULL | |
| narration | varchar(255) | YES | | NULL | |
| voucher_number | varchar(255) | YES | | NULL | |
| debit_credit | float | YES | | NULL | |
| profit | float | YES | | NULL | |
| account_code | int(11) | YES | | NULL | |
| balance | float | YES | | NULL | |
| branch | varchar(255) | YES | | NULL | |
| account_id | int(11) | YES | MUL | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+---------------------+--------------+------+-----+---------+----------------+
16 rows in set (0,01 sec)
mysql> ALTER TABLE transactions ADD FOREIGN KEY (account_id) REFERENCES accounts(id);
Query OK, 0 rows affected (0,03 sec)
Records: 0 Duplicates: 0 Warnings: 0
accounts table
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| account_number | int(11) | YES | | NULL | |
| account_code | int(11) | YES | | NULL | |
| branch | varchar(255) | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
| father_name | varchar(255) | YES | | NULL | |
| nic_number | varchar(255) | YES | | NULL | |
| mohalla | varchar(255) | YES | | NULL | |
| village | varchar(255) | YES | | NULL | |
| nominee | varchar(255) | YES | | NULL | |
| relationship | varchar(255) | YES | | NULL | |
| opening_balance | float | YES | | NULL | |
| opening_date | datetime | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
After running the query shouldn't the column say F_K or something like PRI ?
Any help would be great. Thanks!