Having trouble with logic for a drink recipe database - mysql

I am in the process of developing a database for an automated drink mixing robot, and I'm getting stuck with how to create the database.
In one table, I have a list of every possible ingredient (Coca-Cola, Pepsi, Smirnoff - Red Label, Skyy, etc.). A second table, lists which ingredients are available. A third table specifies the mixed drink recipes. My issue arises with the mixed drink table.
For example, if I wanted to order a Screw Driver, the robot would look to see if any vodka was available, and if any orange juice was available. However, if a drink recipe called for a specific type of vodka (such as Smirnoff - Red Label) I would want the robot to only use Smirnoff and not just any type of vodka.
So, does anyone have any suggestions as to how I could go about developing the database for such an application? I am thoroughly stumped with this one.
Thanks in advance.

It sounds like you need some kind of hierarchy for your ingredients. So you would have "generics" like "cola", "vodka", "rum" etc and then the "brands" like "Coca-Cola", "Smirnoff", "Bacardi" etc
One way of achieving this is with two tables:
Generics
ID - Name
1 - Cola
2 - Vodka
etc
Ingredients
ID - Generic_id - Name
1 - 1 - Coca-Cola
2 - 1 - Pepsi
3 - 2 - Smirnoff - Red Label
4 - 2 - Absolut
etc
Ps This sounds suspiciously like homework. If it is you should tag it as such...

You should add one more table "ingridient category" (ex. fruit juice, vodka, tequila) and reference ingredients table to this with FK. Then alter you receipt table, it should contain category or exact ingridient as a part of receipt.
Then, when robot looks for a ingridients it can decide, if he has exact ingridient or can choose from category list.

Split the ingredients into two tables: ingredient_types (i.e. vodka, juice, soft drink) and ingredients (i.e. Smirnoff, orange juice, cola). In the recipes reference either an ingredient type ("vodka") or specific ingredient ("Smirnoff").

Related

How to find a spell data in acore_world DB

When debugging the SpellEffects.cpp in AzerothCore I found spell_id for example 2006 for level 1 resurrection priest spell and 10880 for level 3. There is a spell_ranks table that relates the two. However I cannot find the additional spell data for either of them. There is a spell_dbc table that seems to contain many columns for spell attributes but these spells do not exist there under ID column where ID = 2006 or ID = 10880.
Where is the data for those spells? specially damage(heal), mana cost, and casting time?
spell_dbc only contains overrides of spells, not the full spell list
more details about this table are available at:
https://www.azerothcore.org/wiki/spell_dbc

SQL To Pull Linked List(ish) Data From Tables

Thank you in advance for your help...
I want to get the parent site list/informatoin for a site based on its ID. There is a site hierarchy described in 2 tables, like this:
table: site
- id (PK)
- name
table: hierarchy
- siteid (PK) (siteid => site.id)
- parented (PK) (parented => site.id)
- topid (topid => site.id)
For example, you may have the following data in the tables:
site:
id,name
0,Earth
1,US
2,NY
3,GA
4,Queens
5,Farmers' Market
6,Buckhead
hierarchy:
siteid,parented,topid
5,4,1
4,2,1
2,1,1
1,0,1
5,6,1
6,3,1
3,1,1
1,0,1
I want to get all the parents of the Farmers's Market sites. There are markets in both Buckhead and Queens.
How do I make SQL walk / recurse up the tree to get something like this if I want the parent list for site id=5?:
site_name,parent_name
Farmers' Market,Queens
Queens,NY
NY,Earth
Farmers' Market,Buckhead
Buckhead,GA
GA,Earth
It occurs to me that there may need to be more than one row in the site table for Farmers' Market, but, I don't think so...
Thanks for your help,
David

SQL self-join to return specific rows

Skip to bottom to avoid long-winded explanation
Ok, so.
I'm working on a company intranet for managing client jobs. Jobs are comprised of Elements: an example element might be "Build a six-page website", or "Design a logo".
Each element consists of a collection of role-hours, so "Build a six-page website" might include four hours of "Developer" rate and two hours of "Designer" rate (ok, maybe a little longer :)
Obviously, different clients get different hourly rates. And, although that's already accounted for in the system, it's not giving us enough flexibilty. Traditionally, our account managers have been rather... ad hoc... with their pricing: the "Build a six-page website" element might include the standard four hours of developer for client "Bob", but eight hours for client "Harry".
Bear with me. I will get to actual code soon.
Elements are, of course, stored in the "Elements" database table - which is composed of little more than an ID and a text label.
My work-in-progress solution to the "we need client-specific elements" problem is to add a "client" field to this table. We can then go through and add any client-specific versions of the available elements, tweaking them to taste.
When the account managers go to add elements to their jobs, they should only see elements that are either (a) available to anyone - that is, they have a NULL client field, or (b) specific to the job client.
So far, so SELECT WHERE.
But that isn't going to cut it. If I add a second "Build a six-page website" element specifically for Harry, then an account manager adding elements to a job for Harry will see both the standard version, and Harry's version of the element. This is no good. They should only see the standard version if there's not an applicable client-specific version.
Ok... soooo: as well as adding a "client" field to the elements table, add a "parent element" field. We can then do something magically self-referential involving joining the table to itself, and fetch only the relevant roles.
My long-awaited question is thus:
Oh look, an actual question
id label client parent_element
1 Standard Thing NULL NULL
2 Harrys Thing 1 1
3 Bobs Thing 2 1
4 Different Thing NULL NULL
Given this table structure, how can I write a single SQL query that will accept a "client ID" parameter and return:
For client ID 1, rows 2 and 4
For client ID 2, rows 3 and 4
For client ID 42, rows 1 and 4
For extra bonus points, the results should include the parent element label. So for client ID 1, for example:
id label standardised_label client parent_element
2 Harrys Thing Standard Thing 1 1
4 Different Thing Different Thing NULL NULL
SELECT mm.*, md.label AS standardized_label
FROM mytable md
LEFT JOIN
mytable mc
ON mc.parent_element = md.id
AND mc.client = #client
JOIN mytable mm
ON mm.id = COALESCE(mc.id, md.id)
WHERE md.client IS NULL
Create an index on (client, parent_element) for this to work fast.
See SQLFiddle.

USING PDO & MYSQL to get rows compared and switched to columns

I have a Products table, an Imprint table, a Manufacturer table and an ingredients table - along with other tables.
I am currently generating information and use what is called "file_name" as the id (it is a FDA assigned 36-40 digit id that goes with each product). What is happening now is people enter of few letters and the autocomplete dropdown provides the exact spelling. When they click SUBMIT and it brings up all entries that match. If they select a generic product it also includes the "brand" product - if they enter a brand it also includes the generic.
The current display is as follows:
Sold by: Sold using Available Chemical name Data based
name Since: on company
report submitted:
C----- Health Lortab 2011-01-13 Hydrocodone Bitartrate etc 2010-12-07
R—Distributors Hydrocodone 2010-02-18 Hydrocodone Bitartrate etc 2009-12-17
Bitartrate
And Acetaminophen
C-- Health Vicodin 1983-01-07 Hydrocodone Bitartrate etc 2009-11-03
R—Distributors Hydrocodone 2010-07-30 Hydrocodone Bitartrate etc 2010-12-28
Bitartrate
And Acetaminophen
This is working fine. I will be adding a check box on the left which will allow up to 3 of the products to be chosen for comparison and additional information.
THE NEXT STEP:
After they check 1, 2 or 3 items, I want to display like this:
"ALL of the versions of the product you checked contain the following ingredients:"
(Those ingredients common to all chosen products).
ACETAMINOPHEN, CELLULOSE, CORN, CROSPOVIDONE, HYDROCODONE BITARTRATE, MICROCRYSTALLINE, STARCH and STEARIC ACID
(Show the ingredients in each product NOT held in common by all.
End result in COLUMNS)
“IN addition, EACH of the products you chose have the following ingredients:"
COLUMN 1(Product 1) COLUMN 2 (Product 2) COLUMN 3 ( Product 3)
COPOVIDONE MAGNESIUM STEARATE CROSCARMELLOSE SODIUM
CROSCARMELLOSE SODIUM POVIDONE D&C YELLOW NO. 10
D&C RED NO. 27 SILICON DIOXIDE FD&C BLUE NO. 1
D&C RED NO. 30 POVIDONE
HYDRATED SILICA SILICON DIOXIDE
MAGNESIUM STEARATE SUCROSE
End of display
Ingredient table: there are 20,000 rows,46 columns. Each row is a different drug and contains the id, file-name and then the ingredients, Each drug (row) has a different combination and number of ingredients. Unused fields are marked "Null";:
Ingredient columns in each row:
id, file_name, 0_gred, 1_gred, 2_gred, 3_gred, 4_gred, 5_gred, 6_gred, 7_gred, 8_gred, 9_gred, 10_gred, 11_gred, 12_gred, 13_gred, 14_gred, 15_gred, 16_gred, 17_gred, 18_gred, 19_gred, 20_gred (etc. up to 43_gred)
The question is how and what approach to use to get the format I need for the ingredients. I have experience in developing registration systems, I am fairly knowledgeable in PHP and am starting to get use to PDO. My Sql experience is minimal and is basically on a "need to know" basis.
I have wondered whether I should focus on a monster of a MySQL query or more on the php side. I thought about a query giving the ingredients common to all 3 and then subtracting that result from each individual drug list to get Part II but that appears to be quite advanced mysql – especially since I need the data to switch from row to COLUMN layout. Any help?
Another idea was to do a query that concatenated the ingredients of each row and then doing array procedures on the php side. Problems, I am having a hard time finding the right code to pull the ingredients out of each row since the number of “NULL” fields compared to “used” fields varies with each row. (How to count null COLUMNS in PDO query? I’ve tried and get the full count of columns in the table.)
To me, this is a braintwister with several steps. I’m looking for the “magic” MySql Code (if it exists) and or suggestions as to what approach (using php, mysql, PDO) you would pursue.
Your interest/help is appreciated!!
Laura
I figured out a php way to do what I needed. I'm sure there is a faster MySql way. If you know of one please share! Laura
First, I set the Mysql empty values to default to "no_ingre".
Then I did the following code 3 times for the 3 choices:
SELECT 'all the ingredient fields WHERE id=$name_of_id'
$result=$stmt->fetch(PDO::FETCH_ASSOC);
foreach($result as $key => $value) {
if ($value == "no_ingre") unset($result[$key]);
}
FIND THE ingredients COMMON TO ALL:
$same_detect= array_intersect($result, $result2, $result3);
$p_same=(implode(", ", $same_detect));
echo "<br />The below ingredients are in all of the chosen products<br />";
echo $p_same;
To FIND THE ONES NOT COMMON TO ALL: Do the below code 3 times (for each ingredient array):
foreach ($result as $item) {
if (!in_array($item, $same_detect, true)) {
echo ("<tr><td>" . $item . "</td></tr>"); }

How to design a database for an Internationale website?

Consider an Internationale website. Let's say this website got some articles. Articles got some common parts between every localized website.
Like for example: ArticleID, Data, Author, CategoryID
+a field (or more) that contains the localized text for this article.
How would you design this table? 2 tables: the first containing the shared article "meta data" (look up) and the other containing a record for every localized version? Like this:
LocalizedID text language ArticleID
1 "text in EN" "en" 1
2 "text in FR" "fr" 1
or a big table containign metadata + localized text? like this:
ArticleID - Data - Author - CategoryID - LocalizedID - TextEN - TextFR - etc
(consider the database is not huge, less than 50.000 articles)
thanks everyone
I think you should go for the first option.. if you ever want to expand (more languages or something) it is easier...