Here again with another problem. This time I drank all the beer, so on offer today is a free virtual meal in my virtual resturant ...
This is a "just for fun" project to try and learn a bit more about php and databases. There is not really a true application for it, but my senario is a resturant who wants to predict how much stock to buy on the basis of a historical data collection.
I have 2 tables in my mysql database. one is called "people", the other is called "ingredients".
People has a few columns, the 3 important ones for this problem are "ID", "FIRST_NAME", and "SECOND_NAME",
ID........FIRST_NAME.....SECOND_NAME
1_____Alan____________Jones
2_____Brian___________Green
3_____Chris___________Wilson
4_____Diane___________Smith
Ingredients has 3 important columns - "SWEET", "SAVORY" and eatdate
SWEET.......SAVORY.....eatdate
3__________1_________2019-2-3
2__________4_________2019-2-4
1__________1_________2019-2-5
1__________4_________2019-2-6
2__________1_________2019-2-7
The numbers in this table refer to the name of the person who ate it
What I would like to do is get a list of peoples names with a number of how many times they eat a type of food within a given date range which is set earlier in the script in variables called '$start1' and '$end1'. What I wnat is something like this:
Alan Jones ate 2 sweet and 3 savory
Brian Green ate 2 sweet and 0 savory
Chris Wilson ate 1 sweet and 0 savory
Diane Smith ate 0 sweet and 2 savory
What code do I need to make this work please? I think I am almost there with this:
$query = "SELECT * , COUNT(SWEET) AS sweet, COUNT(SAVORY) AS savory
FROM people, ingredients
WHERE people.ID=ingredients.SWEET
OR people.ID=ingredients.SAVORY
AND Date BETWEEN '$start1' AND '$end1'
GROUP BY people.ID
";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result))
{
echo $row['FIRST_NAME'].' '.$row['LAST_NAME'].' ate '.'('.$row['sweet'].') sweet and '.'('.$row['savory'].') savory.<br />';
}
but what I get is:
Alan Jones ate 2 sweet and 2 savory
Brian Green ate 2 sweet and 2 savory
Chris Wilson ate 1 sweet and 1 savory
Diane Smith ate 0 sweet and 0 savory
Where am I going wrong and could you explain your answers so I can learn from this please?
Please hurry with this as the virtual food reward wont keep hot forever and my wife didnt slave over a hot virtual stove for nothing!!! :^)
- Posts
- 999
- Registration date
- Saturday January 17, 2015
- Status
- Moderator
- Last seen
- August 11, 2019
195 -Can I interest you in the soup of the day? It's Mango and Cucumber.
- Posts
- 12936
- Registration date
- Monday June 3, 2013
- Status
- Moderator
- Last seen
- September 3, 2019
1342 -You also do understand that count counts the line, so if a person had both on same day, it will only count one or the other, not both, because count counts the recordset line as long as it is not null.
- Posts
- 999
- Registration date
- Saturday January 17, 2015
- Status
- Moderator
- Last seen
- August 11, 2019
195 -- Posts
- 12936
- Registration date
- Monday June 3, 2013
- Status
- Moderator
- Last seen
- September 3, 2019
1342 -try removing the savory and sweet types on the table, and use whatate instead. Then the sql is simple to het what ypu want. Understand, the modelbypu are testing is not a normalized database, as if you wanted to add in SPICY, you would have to enter in another column on the tablex wrecking the dataset as the new colum will be full of NULLs onthe Spicy. It would be much easier to scale if you added in another tastetype of spicy and used the tastetype.id for keeping track on the whatate table.
I do understand that you are trying to keep it as, but.....
- Posts
- 999
- Registration date
- Saturday January 17, 2015
- Status
- Moderator
- Last seen
- August 11, 2019
195 -Please thank your technologist for me. I do appreciate the help. You can upgrade to a full 3 course meal for both of you and your partners - But do keep in mind ... my wife is the head chef - you need to be Sherlock Holmes to know what she gives you.