Check latest id and add 1 [closed] - mysql

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Since I got several "projects" that should contain many questions each, I have a question-page where I fill a videolink, four answers and four drow-down lists where the user is able to set points for every answer.
However, in the database I have two tables.
When I fill and execute my question page I have made so every answer gets an id. In the table "answer_det", Pid, Aid, answer and points is being set. This is how it looks like:
The "question" table when I insert the first question for the first project(Pid=1) :
What I want to do now is to also set the qid(question-id). I'm not sure how to do it, but I think that I should have a code that checks the maximum qid of the pid and add 1 to it so every new question for the same project get a new qid. If the pid isn't in the table, then the qid should get the value "1".
So if you look at the first picture, the qid should be 1 on every showed row since all the four answers belongs to the same question, which is the first one for the project with pid=1. So if I would like to add a question to the same project, it should look the same but with the qid=2 and so on. If I then add a new(first) question for the project 2, the qid should begin on 1 and so on. Then, if i would like to add a new question again for the first project, the code should check that the maximum qid is 2 where pid is 1, and then insert a new question with answers but with qid=3.
It should work the same way on the table "question", which you see on the second picture. When the first question is created, I want the first question for the first project(the one with pid=1) to also have qid=1 and the link that I filled. The second question for the project with pid=1 should then get qid=2. If i add a first question for a new project, it should then be pid=2 and qid=1.
This is the code that I have now, and nothing of it inserts anything in the qid in neither of the two tables.

I suppose you have a predefined set of projects:
Project 1> Science
Project 2> Maths
Project 3> History.....and so on.
I suggest you create a separate table for projects that would have projectID and projectName columns.
Now in your question-page, have these projects in a dropdown(select option) format with projectID as the option value and projectName as the dropdown select name.
Now you have a select option for project name, an input field for question_link and other select options for answers and their weight-age. Now when you insert this form (this question) try coding the following logic (I will just try to write in pseudo-code manner)
//first check if this project already has questions in the question table.
So do something like
SELECT* FROM question WHERE pid = $post['projectID'] ORDER BY qid DESC
// or you could do SELECT TOP 1
// CASE1: if this project never had any questions, the select would return null.
//Hence this is a first question for this project. Then simply write an insert query that
//would insert $post['projectID'] to pid and 1 in qid (since this is the first question).
//Also, insert a series of rows in the answer_det table that will have $post['projectID']
//to pid and 1 for qid and the answer and points as submitted.
// CASE2: if this project already has 1 or more questions, your select query from above
//will return some rows and if you pick $row[0]['qid'], this would be the highest number
//value for question (qid) as we selected using ORDER BY qid DESC. Now, simply do inserts
//in both the tables like you would do as above but this time, in the qid field in both
//the tables, instead of inserting 1, you would insert qid => $row[0]['qid'] + 1.
//You wouldn't need to know what the qid was, all you need is to get the last qid and add
//1 to it.
//Also, you can create a separate page to add the projectName as you feel to add new
//projects and insert this into the project_table and this will begin to show up in the
//select options when you are adding new questions.

Related

Should I normalise my database in this case? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I got stuck with one problem in my website's database. I hope that I explained it well enough below:
My website's idea is to show prices in different shops for the same product. For example I have product "shoesA" it is sold in shopA, shopB, and shopC.
The prices for shoesA are as follows:
shopA : 107$,
shopB : 114$,
shopC : 97$
I can see 2 major ways of storing data about the product:
1) Store ALL the data regarding a single product in a single table, in a single row. This will require using 2D arrays.
Example:
ID=48,
Name="shoesA",
Brand="brandA",
Prices="[["shopA" , 107] , [["shopB " , 114] , [["shopC " , 97]]",
2) Divide this all into separate tables.
Example:
table_product_info
ID=48,
Name="shoesA",
Brand="brandA"
table_product_prices_shopA
ID=4,
Product_ID=48,
Price=107
table_product_prices_shopB:
ID=4,
Product_ID=48,
Price=114
table_product_prices_shopC
ID=4,
Product_ID=48,
Price=97
Why do I think that 1 is faster, hence, better than 2:
To get the entire data for shoesA using method 1 I only need to perform 1 query. But for method 2 it will possibly be 10 queries.
Thank you very much if you spent your time reading this and trying to help!
both are not quite right.
you want the product table to list the product details.
then you want the store table to list the store details.
then finally you need one to tell which product is sold by which store something like this:
store_product
______________
store_id
product_id
begin_date
end_date
price

Converting E-mail to Names [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am attempting to create a script or figure out a formula that will allow me to do the following.
I am a teacher and often have student use Google Forms for quizzes and different surveys. When grading it I normally split their email. Occasionally some students will have a number after their last name.
firstname.lastname#stu.county.stateschools.us
I would like to take their e-mail and convert it to the following format.
lastname, firstname
This would allow me to sort easily and put into gradebook much faster.
The current best route I know of to do this is to split via . , # then join the data I want.
This takes multiple different columns to complete my task that could very easily overwrite their data. I want this to all take place in one column and get rid of the extra information I do not need.
There are multiple ways to do that. I would probably start with the indexOf() "#"
slice() the string to get the "firstname.lastname"
Next, split() the string to separate the firstname and lastname
Now reverse() the order of ["firstname", "lastname"]
Finally, join() them back together
var email = "firstname.lastname#stu.county.stateschools.us";
var index = email.indexOf('#');
var name = email.slice(0, index).split('.').reverse().join(', ');
// Logs "The student name is: lastname, firstname"
Logger.log("The student name is: %s", name);

Rails form with dynamic test bank

I'm looking to create a question bank where the test creator can specify the number of question they would like to see for each category of the form.
Currently I loop through all of the question to build the form, and I'm not sure how to simply pull out only x number of questions per category.
#questions = AuditQuestions.where(question_active: 'Y', audit_forms_id: session[:audit_forms_id]).order(question_order_number: :asc)
Table structure:
id audit_forms_id question_category question_title question_text question_order_number question_active

Rails get the id of the inserted row (not the last inserted) [duplicate]

This question already has answers here:
How to get id values after saving an ActiveRecord model in rails
(3 answers)
Closed 9 years ago.
In my model I have code that looks like this:
Report = Report.new()
Report.Name = name
Report.save()
This process happens several times in a second in parallel. If I get the last inserted id it gives me the wrong results. I don't want the id of the last inserted row in the database, I want the id of the actual inserted row. How do I do this?
report = Report.new
report.name = name
report.save
p report.id # here you get the id
The function required is mysql_insert_id().
More details here -: mysql docs

MySQL: showing totals

I am trying to figure out how to have PHP check and print 2 different functions.
Both of these questions are referring to table called "remix".
The first, and more important problem at the minute, is I would like to know how to show how many DIFFERENT values are under "author", as to compile the amount of total authors registered. I need to know not only how to most efficiently use COUNT on returning UNIQUE names under "author", but how to show it inline with the total number of rows, which are currently numbered.
The second question would be asking how I would be able to set up a top 3 artists, based on how many times their name occurs in a list. This also would show on the same page as the above code.
Here is my current code:
require 'remix/archive/connect.php';
mysql_select_db($remix);
$recentsong = mysql_query("SELECT ID,song,author,filename FROM remix ORDER by ID desc limit 1;");
$row = mysql_fetch_array($recentsong);
echo'
<TABLE BORDER=1><TR><TD WIDTH=500>
Currently '.$row['ID'].' Remixes by **(want total artists here)** artists.<BR>
Most recent song: <A HREF=remix/archive/'.$row['filename'].'>'.$row['song'].'</A> by <FONT COLOR=white>'.$row['author'].'</FONT>
So as you can see, I have it currently set up to show the most recent song (not the most efficient way), but want the other things in there, such as at least the top contributor, but don't know if I would be able to put it all in one php block, break it, or be able to do it all within one quarry call, with the right code.
Thanks for any help!
I'm not sure I really understood everything in your question but we'll work this through together :p
I've created an SQLFiddle to work on some test data: http://sqlfiddle.com/#!2/9b613/1/0.
Note the INDEX on the author field, it will assure good performance :)
In order to know how to show how many DIFFERENT values are under "author" you can use:
SELECT COUNT(DISTINCT author) as TOTAL_AUTHORS
FROM remix;
In order to know the total number of rows, which are currently numbered you can use:
SELECT COUNT(*) as TOTAL_SONGS
FROM remix;
And you can combine both in a single query:
SELECT
COUNT(DISTINCT author) as TOTAL_AUTHORS,
COUNT(*) as TOTAL_SONGS
FROM remix;
To the top 3 subject now. This query will give you the 3 authors with the greatest number of songs, first one on top:
SELECT
author,
COUNT(*) as AUTHOR_SONGS
FROM remix
GROUP BY author
ORDER BY AUTHOR_SONGS DESC
LIMIT 3;
Let me know if this answer is incomplete and have fun with SQL !
Edit1: Well, just rewrite your PHP code in:
(...)
$recentsong = mysql_query("SELECT COUNT(DISTINCT author) as TOTAL_AUTHORS, COUNT(*) as TOTAL_SONGS FROM remix;");
$row = mysql_fetch_array($recentsong);
(...)
Currently '.$row['TOTAL_SONGS'].' Remixes by '.$row['TOTAL_AUTHORS'].' artists.<BR>
(...)
For the top3 part, use another mysql_query and create your table on the fly :)