0

I would like to update several database results in one row.

My Code:

sql_client = "SELECT pd.prj_id, pd.dl_id, pd.name, pd.email FROM projects_doc pd JOIN reminder rm ON pd.prj_id = rm.prj_id WHERE doc_typ = 'assignment' ORDER BY pd.id ASC;"
result = cursor.execute(sql_client)
result = cursor.fetchall()

for row in result:
       sql_prj_id = row['prj_id']
       sql_dl_id = row['dl_id']
       sql_dl_name = row['name']
       sql_dl_email = row['email']

Example Output:

How can I update my table so that the following result comes to an end?

Table structure

prj_id, dl_id, dl_name, dl_email

End result (example)

1
  • What do you mean update? Do you want that output in Python or to overwrite existing MySQL table data? If the latter, how to handle different rows? Commented Jan 2, 2018 at 21:52

1 Answer 1

1

Consider MySQL's dialect-specific aggregate function group_concat:

SELECT pd.prj_id, 
       GROUP_CONCAT(pd.dl_id) AS grp_ids, 
       GROUP_CONCAT(pd.name) AS grp_name, 
       GROUP_CONCAT(pd.email) AS grp_email 
FROM projects_doc pd 
JOIN reminder rm ON pd.prj_id = rm.prj_id 
WHERE doc_typ = 'assignment' 
GROUP BY pd.prj_id
ORDER BY pd.id ASC;

However, it is not clear how you want to run update since more rows would be in original table than above aggregate query. Consider creating a new table from above query:

CREATE TABLE new_table AS <above SELECT query>

Or better yet keep your original long format which provides more efficient storage, scalability, query maintainability, and referential integrity as nested lists in columns are usually for reporting than table structure. And simply create a view from this query. Views are stored queries for use elsewhere even in application level like Python and can even be queried themselves.

CREATE VIEW new_view AS <above SELECT query>

SELECT * FROM new_view WHERE prj_id = 1;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.