Im just new to MySql Programming and Im creating a project to enchance my skills.
I have a table called tblRecipe which has the columns RecipeId, RecipeName and Instructions. the other table is called tblIngredients which has the columns IngredientId,RecipeId, IngredientName.
Now for example,RecipeOne needs IngredientOne and IngredientTwo to make, RecipeTwo needs IngredientOne,IngredientTwo and IngredientThree to make.
My programs asks the IngredientNames for the input so when i put in IngredientOne and IngredientTwo it should give me back the result RecipeOne.
Here is my code
Select Recipename,Instructions
from tblREcipe where RecipeId in
(select recipeID
from tblIngredients
where Ingredientname in('IngredientOne','IngredientTWo')
);
I know that the IN operator is like saying match any of the following.
So with that code it gives me both recipes.
Im looking for an equivalent of AND.
I tried "Ingredientname =ALL(in('IngredientOne','IngredientTwo'))"
but it does not return any rows.
Im open to any changes such as restructuring the tables if that fixes the problem since this is just a practice project.Hope to hear from you soon guys and thank you in advance.
You also need to count the number of instance of the records that match to the number of ingredients. Like this one below:
Select a.Recipename,
a.Instructions
FROM tblREcipe a INNER JOIN
(
SELECT recipeID,
COUNT(IngredientId)
FROM tblIngredients
WHERE Ingredientname IN ('IngredientOne', 'IngredientTWo')
GROUP BY recipeID
HAVING COUNT(IngredientId) = 2 -- the number of ingredients
-- should somehow dynamic
) b ON a.recipeID = b.recipeID
Related
I have multiple tables in my project and I want to count out something from a table which is video table , first let me attach the table Images...
above is table_std
above is tablesubject
above is table_chapter
above is table video
now I want to fund total sum of videotime which is in the tbvid and want it against stdid like below
so here first it all starts with standard table , next table subtable is dependent of std table then chap table is depends over sub table and vid table depends over chap table.
for me the sql query is going to be very difficult if anyone can help me with this , it will be so appreciated !
Refer from
SUM(subquery) in MYSQL
SELECT `std`.*,
(SELECT SUM(`video`.`vidtime`) FROM `video` WHERE `vidid` = `std`.`stdid`) AS `sum_vidtime`
FROM `table_std` AS `std`
If you want to use only these columns: stdid, vidtime (sum_vidtime in my case) then it is no need to join multiple tables. Use sub query.
And as everyone said, please don't use image for an example.
I am fairly new to Databases and I am just beginning to understand the DML/queries, I have two tables, one named customer this contain customer data and one named requested_games, this contains games requested by the customers, I would like to write a query that will return the customers that have requested more than two games, so far when I run the query, I don't get the desired result, not sure if I'm doing it right.
Can anyone assist with this thanks,
Below is a snippet of the query
select customers.customer_name, wants_list.requested_game, wants_list.wantslists_id,count(wants_list.customers_ID)
from customers, wants_list
where customers.customers_ID = wants_list.customers_id
and wants_list.wantslists_id = wants_list.wantslists_id
and wants_list.requested_game > '2';
just include a HAVING clause
GROUP BY customers_ID
HAVING COUNT(*) > 2
depending on how you have your data setup you may need to do
HAVING COUNT(wants_list.requested_game) > 2
This is how I like to describe how a query works maybe itll help you visualize how the query executes :)
SELECT is making an order at a restaurant....
FROM is the menu you want to order from....
JOIN is what sections of the menu you want to include
WHERE is any customization you want to make to your order (aka no mushrooms)....
GROUP BY (and anything after) is after the order has been completed and is at your table...
GROUP BY tells your server to bring your types of food together in groups
ORDER BY is saying what dishes you want first (aka i want my entree then dessert then appetizer ).
HAVING can be used to pick out any mushrooms that were accidentally left on the plate....
etc..
I would like to write a query that will return the customers that
have requested more than two games
For this to happen you need to do the following
First you need to use GROUP BY to group the games based on customers (customers_id)
Then you need to use HAVING clause to get customers who requested more than two games
Then make this a SUBQUERY if you need more information on the customer like name
Finally you use a JOIN between customers and the sub query (temp) to display more information on the customer
Like the following query
SELECT customers.customer_id, customers.customer_name, game_count
FROM (SELECT customer_id, count(wantslists_id) AS game_count
FROM wants_list
GROUP BY customer_id
HAVING count(requested_game) > '2') temp
JOIN customers ON customers.customer_id = temp.customer_id
I have to create a table where there are lots of columns pulling date from one table and two views. To display the columns is no problem. The problem comes into play when I have to alias two columns to show a patient who received services from the first two distinct doctors (doctor_1 and doctor_2). Each doctor has their own ID. I’m not sure if I should use the “distinct top (2)”, “rank”, etc… Here is what it sort of looks like.
Date, LastName, FirstName, Address, City, State, Zip, Hostital, Room, Doctor_1,Doctor_2
Any help would be greatly appreciated.
Let's assume your table looks something as follows:
doctor_patients:
doctor_id int,
patient_id int,
visit_date datetime
In that case you can write a query similar to the following:
select a.patient_id, b.doctor_id as doctor_1, b.visit_time_max as doctor_1_visit_time, a.doctor_id as doctor_2, max(a.visit_time) as doctor_2_visit_time
from doctor_patients a
inner join
(select patient_id, doctor_id, max(visit_time) as visit_time_max
from doctor_patients
where visit_time < a.visit_time
and patient_id = a.patient_id
and doctor_id = a.doctor_id) b
You can always put a where clause at the tail end and apply filters to the "a" table. I am not sure if this query will run as-is because I did not try it out on SQL. But I hope this gives you a general idea about what you need to do. There might be better ways to accomplish this using latest SQL features but this should get you rolling. I hope this is the answer you were looking for!
I have three tables: students, interests, and interest_lookup.
Students has the cols student_id and name.
Interests has the cols interest_id and interest_name.
Interest_lookup has the cols student_id and interest_id.
To find out what interests a student has I do
select interests.interest_name from `students`
inner join `interest_lookup`
on interest_lookup.student_id = students.student_id
inner join `interests`
on interests.interest_id = interest_lookup.interest_id
What I want to do is get a result set like
student_id | students.name | interest_a | interest_b | ...
where the column name 'interest_a' is a value in interests.name and
the interest_ columns are 0 or 1 such that the value is 1 when
there is a record in interest_lookup for the given
student_id and interest_id and 0 when there is not.
Each entry in the interests table must appear as a column name.
I can do this with subselects (which is super slow) or by making a bunch of joins, but both of these really require that I first select all the records from interests and write out a dynamic query.
You're doing an operation called a pivot. #Slider345 linked to (prior to editing his answer) another SO post about doing it in Microsoft SQL Server. Microsoft has its own special syntax to do this, but MySQL does not.
You can do something like this:
SELECT s.student_id, s.name,
SUM(i.name = 'a') AS interest_a,
SUM(i.name = 'b') AS interest_b,
SUM(i.name = 'c') AS interest_c
FROM students s
INNER JOIN interest_lookup l USING (student_id)
INNER JOIN interests i USING (interest_id)
GROUP BY s.student_id;
What you cannot do, in MySQL or Microsoft or anything else, is automatically populate columns so that the presence of data expands the number of columns.
Columns of an SQL query must be fixed and hard-coded at the time you prepare the query.
If you don't know the list of interests at the time you code the query, or you need it to adapt to changing lists of interest, you'll have to fetch the interests as rows and post-process these rows in your application.
What your trying to do sounds like a pivot.
Most solutions seem to revolve around one of the following approaches:
Creating a dynamic query, as in Is there a way to pivot rows to columns in MySQL without using CASE?
Selecting all the attribute columns, as in How to pivot a MySQL entity-attribute-value schema
Or, identifying the columns and using either a CASE statement or a user defined function as in pivot in mysql queries
I don't think this is possible. Actually I think this is just a matter of data representatioin. I would try to use a component to display the data that would allow me to pivot the data (for instance, the same way you do on excel, open office's calc, etc).
To take it one step further, you should think again why you need this and probably try to solve it in the application not in the database.
I know this doesn't help much but it's the best I can think of :(
In a soccer environment I want to display the current standings. Meaning: points and goals per team. The relevant tables look similar to the following (simplified).
Match Table
uid (PK) hometeamid roadteamid
------------------------------------------------------------------
Result Table
uid (PK) hometeamscore roadteamscore resulttype (45min, 90min, ..)
-------------------------------------------------------------------
Team Table
uid (PK) name shortname icon
------------------------------------------------------------------
Now I don't get my head around it, how to write the standings in one query. What I managed was to write a query, which returns the "homegames"-standings only. I guess that's the easy part. Anyway here is how it looks:
SELECT ht.name,
Count(*) As matches,
SUM(res.hometeamscore) AS goals,
SUM(res.roadteamscore) AS opponentgoals,
SUM(res.hometeamscore - res.roadteamscore) AS goalDifference,
SUM(res.hometeamscore > res.roadteamscore) * 3 + SUM(res.hometeamscore = res.roadteamscore) As Points
FROM league_league l
JOIN league_gameday gd
ON gd.leagueid = l.uid
JOIN league_match m
ON m.gamedayid = gd.uid
JOIN league_result res
ON res.matchid = m.uid
AND res.resulttype = 2
JOIN league_team ht
ON m.hometeamid = ht.uid
Where l.uid = 1
Group By ht.uid
Order By points DESC, goalDifference DESC
Any idea how to modify this, that it will return home- and roadgames would be big time appreciated.
Many thanks,
Robin
Create views. If your data does not change often and you need performance, create one or more pre-computed tables.
Views in MySQL are juste pseudo-tables that are dynamically computed from a SELECT query. Using the SQL in your question, you can create a view of the teams results at home: CREATE VIEW homegames AS SELECT ...
Then do the same for road games. Then it will be easy to synthesize both views in a third one (you just need to sum up the columns).
Views have at least one flaw: they are slow. A view built on views is like using complex subqueries, and MySQL is quite bad at this. I don't think it's a problem for you as you're probably dealing with hundreds of games at most. But if you find these views to be too slow to query, and provided you don't use any kind of cache that could mitigate this, then use simple tables instead of views. Of course, you'll need to keep them in sync. You can TRUNCATE and INSERT INTO homegames SELECT ... each time you have a new game, or you can be smarter and just UPDATE the tables. Both are right, depending on your needs.
Could you not abstract this out into a stored procedure or stored function to call rather than constructing such a big-ass complicated query?