MySQL Unknown column in where clause? - mysql

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.

Related

MySQL Matching where clause with optional NULL

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;

Update MySQL table based on results for joining to tables

I have three tables, emails, person_details and data_providers. Basically all of my users id, email, and current assigned data_providers_id are stored in the emails table.
The second table, person_details contains demographic information collected by multiple data providers, each row identified by an emails_id that is relational to the emails.id data_providers_id that is relational to the third table data_providers.id
The third table, data_providers contains each of my data providers id, name, and precedence.
Basically, a users information could be collected from multiple sources, and I need to UPDATE emails set data_providers_id = based on a select that would JOIN the person_details table and the data_providers table sorting by data_providers.precedence DESC then person_details.import_date ASC and use the first value (highest precedence, then oldest import_date).
I was trying to build the query, but my subquery is returning more than one row. This query is a little over my head, hoping someone more experienced with complex queries might be able to point me in the right direction.
UPDATE emails
SET emails.data_providers_id =
SELECT person_details.data_providers_id
FROM person_details
LEFT JOIN data_providers ON person_details.data_providers_id = data_providers.id
ORDER BY data_providers.percent_payout ASC, person_details.import_date ASC ;
Here are some details about the three tables if this helps. Any guidance would be MUCH appreciated. Thanks in advance :)
emails table:
+-------------------+---------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------------+------+-----+---------------------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| data_providers_id | tinyint(3) unsigned | NO | MUL | NULL | |
| email | varchar(255) | NO | UNI | NULL | |
+-------------------+---------------------+------+-----+---------------------+----------------+
person_details:
+-------------------+---------------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------------+------+-----+---------------------+-------+
| emails_id | int(11) unsigned | NO | PRI | NULL | |
| data_providers_id | tinyint(3) unsigned | NO | PRI | NULL | |
| fname | varchar(255) | YES | | NULL | |
| lname | varchar(255) | YES | | NULL | |
| address_line1 | text | YES | | NULL | |
| address_line2 | text | YES | | NULL | |
| city | varchar(255) | YES | | NULL | |
| state | varchar(2) | YES | | NULL | |
| zip5 | varchar(5) | YES | | NULL | |
| zip4 | varchar(4) | YES | | NULL | |
| home_phone | varchar(10) | YES | | NULL | |
| mobile_phone | varchar(10) | YES | | NULL | |
| work_phone | varchar(10) | YES | | NULL | |
| dob | date | YES | | NULL | |
| gender | varchar(1) | YES | | NULL | |
| ip_address | varchar(15) | NO | | NULL | |
| source | varchar(255) | NO | | NULL | |
| optin_datetime | datetime | NO | MUL | NULL | |
| import_date | timestamp | NO | | 0000-00-00 00:00:00 | |
+-------------------+---------------------+------+-----+---------------------+-------+
data_providers table:
+-----------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+----------------+
| id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| precedence | int(2) | YES | | 0 | |
+-----------------+---------------------+------+-----+---------+----------------+
To use a SELECT as an expression you have to put it in parentheses. And to get the first value, use LIMIT 1:
UPDATE emails
SET emails.data_providers_id = (
SELECT person_details.data_providers_id
FROM person_details
LEFT JOIN data_providers ON person_details.data_providers_id = data_providers.id
WHERE person_details.emails_id = emails.id
ORDER BY data_providers.percent_payout ASC, person_details.import_date ASC
LIMIT 1) ;

Get the first comment per post

Im using the django comment system and want to fetch only the FIRST comments on posts.
mysql> desc django_comments ;
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| content_type_id | int(11) | NO | MUL | NULL | |
| object_pk | longtext | NO | | NULL | |
| site_id | int(11) | NO | MUL | NULL | |
| user_id | int(11) | YES | MUL | NULL | |
| user_name | varchar(50) | NO | | NULL | |
| user_email | varchar(75) | NO | | NULL | |
| user_url | varchar(200) | NO | | NULL | |
| comment | longtext | NO | | NULL | |
| submit_date | datetime | NO | | NULL | |
| ip_address | char(15) | YES | | NULL | |
| is_public | tinyint(1) | NO | | NULL | |
| is_removed | tinyint(1) | NO | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
my posts are content_type_id = 48.
I want know the user that left the first comment (min submit_date) of each post (object_pk).
Is there a "django" way to do this?
Or need to use raw SQL?
Use a function like this:
def get_first_comment(post):
from django.contrib.comments.models import Comment
from django.contrib.contenttypes.models import ContentType
ct = ContentType.objects.get_for_model(post)
first_comments = Comment.objects.filter(
content_type=ct,
object_id=post.pk,
).order_by("submit_date")
if first_comments:
return first_comments[0]
return None
It loads the content type for your model instance and then filters comments by the content type and object id sorting them by submit date. Then it takes the first comment if a query is not empty.

mysql - adding datetime column and inserting values referenced on another table

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

mysql problem: populate table from another table but referencing ID instead of name

I'm now trying to populate my 'testMatch' table (below) with data from my 'summary' table:
TESTMATCH TABLE
+------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| match_id | int(11) | NO | PRI | NULL | |
| match_date | date | YES | | NULL | |
| ground | varchar(50) | YES | MUL | NULL | |
| homeTeam | varchar(100) | YES | MUL | NULL | |
| awayTeam | varchar(100) | YES | MUL | NULL | |
| matchResult | varchar(100) | YES | MUL | NULL | |
| manOfMatch | varchar(30) | YES | | NULL | |
| homeTeam_captain | int(10) | YES | MUL | NULL | |
| homeTeam_keeper | int(10) | YES | MUL | NULL | |
| awayTeam_captain | int(10) | YES | MUL | NULL | |
| awayTeam_keeper | int(10) | YES | MUL | NULL | |
+------------------+--------------+------+-----+---------+-------+
There is no problem populating match_id -----> manOfMatch - it is 'homeTeam_captain', 'homeTeam_keeper', 'awayTeam_captain' and 'awayTeam_keeper' that i'm having problems bringing in.
SUMMARY TABLE
mysql> DESCRIBE SUMMARY;
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| matchID | int(11) | NO | PRI | NULL | |
| Test | int(11) | YES | | NULL | |
| matchDate | date | YES | | NULL | |
| Ground | varchar(50) | YES | | NULL | |
| HomeTeam | varchar(100) | YES | | NULL | |
| AwayTeam | varchar(100) | YES | | NULL | |
| matchResult | varchar(50) | YES | | NULL | |
| MarginRuns | int(11) | YES | | NULL | |
| MarginWickets | int(11) | YES | | NULL | |
| ManOfMatch | varchar(40) | YES | | NULL | |
| HomeTeamCaptain | varchar(30) | YES | | NULL | |
| HomeTeamKeeper | varchar(30) | YES | | NULL | |
| AwayTeamCaptain | varchar(30) | YES | | NULL | |
| AwayTeamKeeper | varchar(30) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+-------+
I need to somehow select the data from summary, get the corresponding player_id and input the player_id into my 'testMatch'. Player table below:
PLAYERS TABLE
mysql> describe players;
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| player_id | int(11) | NO | PRI | NULL | auto_increment |
| player_surname | varchar(30) | YES | | NULL | |
| team | varchar(100) | YES | MUL | NULL | |
+----------------+--------------+------+-----+---------+----------------+
So to clarify, eg. I want to select homeTeam_captain data FROM summary table BUT not the name, I want the corresponding player_id instead.
I assume I need to use some sort of join/subqueries to get this done... i've tried finding the correct select query first to make sure i'm pulling out the right data, and I have been using the below code for testing (thanks to user Larry_Croft for helping me with this):
select matchID, player_id, player_surname, team from players p, summary s
where
s.hometeamcaptain = p.player_surname AND s.HomeTeam = p.team ORDER BY matchID;
But this correctly brings back 65 rows (65 matches) BUT i then tried it with:
select matchID, player_id, player_surname, team from players p, summary s
where
s.hometeamKEEPER = p.player_surname AND s.HomeTeam = p.team ORDER BY matchID;
But this brings back only 61 rows (should be 65) so i must have an error in the logic.
Once I get this select to work, i then need to somehow include it into my 'INSERT INTO.....SELECT statement to put all the data into 'testMatch' table.
I hope this makes sense and thanks for your help!
Well for me it looks that there is eitehr a hometeamKEEPER that has a null value or that the value of hometeamKEEPER is not in the players table.
Using the following query you should be able to find the hometownKEEPER that are not in the players table:
SELECT matchID, player_id, player_surname, team
FROM players p
RIGHT JOIN summary s ON p.hometeamKEEPER = p.player_surname AND
s.HomeTeam = p.team
ORDER BY matchID;