I need this query to call from PHP:
From a database having
id question_id set_id user_id answered created
I would like to perform:
from all question take those tho have user_id = x and set_id = y
then: for all found number choose only one (the last in time) (distinct question_id)
then: from this list take only the one which has answered = 2 and count how many question are in this list.
let's call this number count1
(this was still easy to do)
Now comes the complicated part (at least for me)
from all question take those tho have user_id = x and set_id = y
then: for all found number choose only one (second last in time) (distinct question_id)
then: from this list take only the one which has answered = 2 and count how many question are in this list.
let's call this number count2
and at the end:
from all question take those tho have user_id = x and set_id = y
then: for all found number choose only one (third last in time) (distinct question_id)
then: from this list take only the one which has answered = 2 and count how many question are in this list.
let's call this number count3
This would be pretty good. It is is possible I would also let the database make this calculation:
count1*3+count2*2+count3*1
You may have better idea to get this calculation done like
for each question get the tree newest (last in time) entry and make the multiplication as I showed => sum up everything
To pay attention:
- you do not know how many different question_id there is in a set
- you will never have them all (like id 23, 24, 30, 80)
- you will not always have tree age level
SQL for testing:
-- phpMyAdmin SQL Dump
-- version 3.3.9.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generato il: 23 apr, 2012 at 01:14 PM
-- Versione MySQL: 5.5.9
-- Versione PHP: 5.3.6
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `stats`
--
-- --------------------------------------------------------
--
-- Struttura della tabella `answers`
--
CREATE TABLE `answers` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned DEFAULT NULL,
`set_id` int(10) unsigned DEFAULT NULL,
`question_id` int(10) unsigned DEFAULT NULL,
`answered` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`created` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=31 ;
--
-- Dump dei dati per la tabella `answers`
--
INSERT INTO `answers` VALUES(1, 1, 1, 1, '1', '2012-04-23 13:07:04');
INSERT INTO `answers` VALUES(2, 1, 1, 2, '2', '2012-04-23 13:07:04');
INSERT INTO `answers` VALUES(3, 1, 1, 3, '1', '2012-04-23 13:07:04');
INSERT INTO `answers` VALUES(4, 1, 1, 4, '2', '2012-04-23 13:07:04');
INSERT INTO `answers` VALUES(5, 1, 1, 5, '2', '2012-04-23 13:07:04');
INSERT INTO `answers` VALUES(6, 1, 1, 6, '1', '2012-04-23 13:07:04');
INSERT INTO `answers` VALUES(7, 1, 1, 8, '2', '2012-04-23 13:07:04');
INSERT INTO `answers` VALUES(8, 1, 1, 10, '2', '2012-04-23 13:07:04');
INSERT INTO `answers` VALUES(9, 1, 1, 11, '1', '2012-04-23 13:07:04');
INSERT INTO `answers` VALUES(10, 1, 1, 12, '1', '2012-04-23 13:07:04');
INSERT INTO `answers` VALUES(11, 1, 1, 1, '2', '2012-04-23 13:07:31');
INSERT INTO `answers` VALUES(12, 1, 1, 2, '2', '2012-04-23 13:07:31');
INSERT INTO `answers` VALUES(13, 1, 1, 3, '1', '2012-04-23 13:07:31');
INSERT INTO `answers` VALUES(14, 1, 1, 4, '2', '2012-04-23 13:07:31');
INSERT INTO `answers` VALUES(15, 1, 1, 5, '1', '2012-04-23 13:07:31');
INSERT INTO `answers` VALUES(16, 1, 1, 6, '1', '2012-04-23 13:07:31');
INSERT INTO `answers` VALUES(17, 1, 1, 7, '2', '2012-04-23 13:07:31');
INSERT INTO `answers` VALUES(18, 1, 1, 9, '1', '2012-04-23
Develop concept website for nutrition log. Visit www.nutriperso.com.br for an initial concept. We prefer .NET C# website and MS SQL database. We will provide a temporary SQL if needed. FTP to upload code to website will be provided and code will be reviewed by MS .NET specialist...
Project Specifications: "
It's basically similar to the famous multiplayer runner game, 'Fun Run' (https://play.google.com/store/apps/details?id=no.dirtybit.funrun). But it differs in many features such as design, interface, items, level-up system and etc (all the details will be provided)...
We are planning to move our current website to Shopify. We are using Microsoft Dynamics GP as our inventory and accounting system. It is using Microsoft SQL for its database...
C#, ASP.NET, JQuery, MVC,Sqlserver Programmer Needed for financial calculator . 1.Loan Calculator If you have a loan, then you probably want to repay your loan as soon as possible, minimizing your interest cost, without sacrificing your quality of life...
This is a small one-off project, ostensibly in Drupal though I'm willing to listen to reasons why another option suits the setup better (on the same budget - I really don't need snotty emails about spending $5k on core php, thank you)...
Want a sample MVC To-do application, which uses SQL Server with Entity Framework, and AngularJS. Todo table Columns: id int, activity string, due datetime, isDone bool One table is sufficient...
We are looking to hire a stellar individual to join our team to provide part-time work centered around the creation of tests for our product in the software education market...
We have a legacy Access 2010 database, rather full-featured...
We are looking for a BizTalk Developer (2010,2013 version) to build one of our applications . Should be able to spend around 40 hours every week or more.. Would be willing to pay 500$ per month...