0

I've got a query that's connected to a table which I only want to see the last entered result based on the dtm.

This is my query:

CREATE OR REPLACE VIEW vwc_students AS 
SELECT u_students.stud_id,u_students.stud_firstname, u_students.stud_middlename,u_students.stud_lastname, CONCAT_WS(' ',u_userse.user_firstname,u_userse.user_lastname) AS stud_editeduser, u_students.stud_edited, u_tbleeditlog.editedDtm
FROM u_students
JOIN u_studsites ON u_studsites.uss_studid = u_students.stud_id
JOIN u_locations ON u_locations.loc_id = u_students.stud_base
JOIN u_users ON u_users.user_id = u_students.stud_loggedid
LEFT JOIN (SELECT editedID, editedDtm FROM u_tbleeditlog WHERE editSite = 1 AND editType = 1 ORDER BY editedDtm DESC LIMIT 1) u_tbleeditlog ON u_tbleeditlog.editedID = u_students.stud_id
LEFT JOIN u_users AS u_userse ON u_userse.user_id = u_tbleeditlog.editedID
WHERE u_studsites.uss_siteid = '1'

but I'm getting the error #1349 - View's SELECT contains a subquery in the FROM clause

I got the code examples from Limiting a left join to returning one result? and MySQL JOIN Multiple Tables With LIMIT Last Table By DateTime per result

Is what I am trying to do possible, and if so how?

Update

I now have the following two views as suggested:

CREATE OR REPLACE VIEW vwc_students AS 
SELECT u_students.stud_id,u_students.stud_firstname, u_students.stud_middlename,u_students.stud_lastname, u_userse.editedEditor AS stud_editeduser, u_userse.editedDtm AS stud_edited
FROM u_students
JOIN u_studsites ON u_studsites.uss_studid = u_students.stud_id
JOIN u_locations ON u_locations.loc_id = u_students.stud_base
JOIN u_users ON u_users.user_id = u_students.stud_loggedid
LEFT JOIN vwu_editlog AS u_userse ON u_userse.editTypeID = u_students.stud_id AND u_userse.editType = 1
WHERE u_studsites.uss_siteid = '1'

and

CREATE OR REPLACE VIEW vwu_editlog AS 
SELECT u_tbleeditlog.*, CONCAT_WS(' ',u_users.user_firstname,u_users.user_lastname) AS editedEditor
FROM u_tbleeditlog
JOIN u_users ON u_users.user_id = u_tbleeditlog.editedID
ORDER BY u_tbleeditlog.editedDtm DESC LIMIT 1

But this last view is limiting the entire thing to 1 result! How would I limit each unique ID to 1? I also get the message Current selection does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available., even though in the original table it is there.

halfer
  • 19,824
  • 17
  • 99
  • 186
dpDesignz
  • 1,909
  • 10
  • 34
  • 70
  • Add an `ORDER BY` clause for the date, and then limit your result set to the first row. – jpaugh Aug 27 '14 at 23:57
  • 1
    As per the accepted answer, this can be marked as a duplicate of: [View's SELECT contains a subquery in the FROM clause](https://stackoverflow.com/questions/8428641/views-select-contains-a-subquery-in-the-from-clause) – halfer Nov 13 '22 at 17:38

2 Answers2

1

Duplicate of: View's SELECT contains a subquery in the FROM clause

As per documentation:

MySQL Docs

  • The SELECT statement cannot contain a subquery in the FROM clause.

Your workaround would be to create a view for each of your subqueries.

Then access those views from within your view view_credit_status

So maybe you'd first do something like this:

CREATE OR REPLACE VIEW u_tbleeditlog AS 
 SELECT editedID, editedDtm
  FROM u_tbleeditlog
  WHERE editSite = 1
  AND editType = 1
  ORDER BY editedDtm DESC
  LIMIT 1;
Community
  • 1
  • 1
Joe T
  • 2,300
  • 1
  • 19
  • 31
1

You are missing the fact that views in MySQL do not allow subqueries in the from clause. They are allowed in the select and where and having clauses, however.

The documentation:http://dev.mysql.com/doc/refman/5.0/en/create-view.html is quite clear:

Subqueries cannot be used in the FROM clause of a view.

In your case, you can probably rewrite the from clause as a correlated subquery in the select clause. You can also use multiple layers of views to do what you want.

Create a separate view for each of the FROM clause.

Suchit kumar
  • 11,809
  • 3
  • 22
  • 44