Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulate new column with values from database MySQL
    primarykey
    data
    text
    <p>I've a MySQL query where I fetch with association a list of users' visits ordered by desc date.</p> <pre><code>SELECT `id`, `name`, `date` FROM `logs` WHERE `iid` = ? ORDER BY `date` DESC'; </code></pre> <p>Each log has <code>iid, id, name, date</code>, the generated array so has the 3 requested values for each row.</p> <p>Here an example of the database:</p> <pre><code>| iid | id | name | date | --------------------------------------------- | 1 | 1 | foo | 2013-09-13 10:14 | | 2 | 1 | foo | 2013-09-12 08:10 | | 2 | 1 | foo | 2013-09-11 14:43 | | 1 | 1 | foo | 2013-09-10 15:12 | </code></pre> <p><code>iid</code> is the page, <code>id</code> the user id.</p> <p>Now I need to add a 4th column, called <code>lastvisit</code> which should contain the date of the previous visit by each user on each <code>iid</code>.<br> So I should have two different <code>lastvisit</code> values for the two <code>iid</code>:</p> <pre><code>| iid | id | name | date | lastvisit | ----------------------------------------------------------------- | 1 | 1 | foo | 2013-09-13 10:14 | 2013-09-12 15:12 | | 2 | 1 | foo | 2013-09-12 08:10 | 2013-09-11 14:43 | | 2 | 1 | foo | 2013-09-11 14:43 | null | | 1 | 1 | foo | 2013-09-10 15:12 | null | </code></pre> <p>I will add this column to the database and at each visit I will check the most recent visit to get the <code>lastvisit</code> value to put in the new entry. </p> <p>My problem is about the already recored entries, which have not the value for <code>lastvisit</code>. </p> <p>I need to populate them with a query, something like:</p> <pre><code>UPDATE `logs` SET `lastvisit` = ( SELECT `date` FROM `logs` WHERE `id` = ? AND 'iid' = ? ORDER BY `date` DESC' LIMIT 0, 1; ) </code></pre> <p>Obviosly this query doesn't work, is it possible to write a query to perform this task for each entry?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload