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

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

Related

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);

Check latest id and add 1 [closed]

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.

Should I go with MySQL or MongoDB? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm in the planing phase of the development of an mobile app and I need your help to decide wheather I should go with MySQL or MongoDB. First let me describe the data:
I have a set of Items (e.g. movies) that users will be able to browse and score in different ways. The total number of Items is approximately 20 000 and it will not grow or shrink.
Each user will be able to score each item (numerical value) and write a comment assigned to each item. I expect that the average user will score maybe 40 different items, but my hopes are higher!
The typcial queries will be
Give me all items that satisfies some criteria and let me browse them. Example: Give me all Thrillers produced in Europe during the 00s.
Give me all movies that a particular user has scored.
I would also like to do some server side calculations e.g. average score for each movie, and each time a user sumbits a new score for an item this average should be updated.
For MySQL I would have a database with 3 different tables for this (Items, Users, Scores) and for MongoDB I'm considering a database with 1 collection, like this:
{
"name":"Inception"
"year":2010
"director":"Christopher Nolan"
"scores" : [
{
"user_id": 1234
"scoreA" : 3,
"scoreB" : 5,
"scoreC" : 4,
},
{
"user_id": 1235
"scoreA" : 4,
"scoreB" : 3
},
}
{
"name":"Titanic"
"year":1997
"director":"James Cameron"
"scores" : [
{
"user_id": 1201
"scoreB" : 5,
"scoreC" : 5
},
{
"user_id": 1220
"scoreA" : 4,
"scoreC" : 5
},
}
...
Question: Given my use case, does it matter if I use MySQL or MongoDB?
Thank you in advance!
Go with a relational database like mysql, there is no need to use MongoDb here unless you just wanna try it out.
As generell advice use NoSQL (especially MongoDb) when you can't improve your RDMS anymore.
Also take a look at this wonderful presentation: http://www.thedotpost.com/2015/06/neha-narula-consistency-and-candy-crush

How to export leads history section in sugarcrm? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm using sugarCRM CE 6.5.14.
I would like to export leads history ( mail details) section. I googled but the details are/were not enough to complete my requirement.
Normal leads > export > not having this history section.
Here is the SQL Query that I used. It works for me.
SELECT l.first_name, l.last_name, l.id, group_concat(n.name), n.description
FROM leads AS l
inner join notes as n on l.id = n.parent_id
where l.deleted = 0 and n.deleted = 0
group by l.id
Hope this will help to some one like me.
There is no way of using the SugarCRM CE UI to export Email records. The closest you can get is to derive the URI index.php?module=Emails&entryPoint=export which will give you the data about an email record (e.g. subject, related to information, status). It won't provide the sender, recipient or email content.
That means you'll need to get into the database or use a GUI reporting tool like JasperReports.
A query that pulls all emails (meta-data and content) from the database would be
select
name,
to_addrs,
cc_addrs,
bcc_addrs,
description,
description_html,
parent_id,
parent_type
from emails
join emails_text on emails.id = emails_text.email_id;
/* where parent_id = '<your lead id>' */
You should know that the Lead History subpanel is populated two ways in regards to Emails. Emails can be directly related to a lead, i.e. parent_id = the lead guid. They can also show up there if the email address matches. This happens if an email is related to an Opportunity or a Contact, but the to_addrs or from_addrs contain an email address that matches an email address that is also on file with the Lead. The above query does not take this non-directly-related emails into consideration. For a query that'll help with that, I recommend you dig into include/utils.php and check out a function called get_unlinked_email_query().

How can I see full order details for each time a certain coupon code was used in magento?

We are trying to collect data on each person that used a certain coupon code "NEWCUSTOMER". We are trying to get the order details including their name, email address, and what they ordered.
Is the rule_id connected to an order in any way in the database? The magento databases don't seem to be all that friendly when you are trying to write your own mySQL statement to figure this information out.
Thanks!
This is similar to your previous question which i have also answered for you: Find the name and email address of non-members who used coupon code in Magento
The coupon code used on an order is actually a property of the order: coupon_code
It sounds from your question that you are directly querying the db, if so then you are looking for the coupon_code field in the sales_flat_order table.
Here is the sql:
SELECT `customer_firstname`, `customer_lastname`, `customer_email` FROM `sales_flat_order` WHERE `coupon_code` = 'your_awesome_coupon_code' AND `customer_group_id` = 0
Or, via magento...
$orderCollection = Mage::getModel('sales/order')->getCollection()
->addAttributeToSelect('customer_firstname')
->addAttributeToSelect('customer_lastname')
->addAttributeToSelect('customer_email')
->addAttributeToSelect('customer_group_id')
->addAttributeToSelect('coupon_code')
->addAttributeToFilter('customer_group_id', Mage_Customer_Model_Group::NOT_LOGGED_IN_ID)
->addAttributeToFilter('coupon_code', 'NEWCUSTOMER');