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.
Related
i want to use pivot query using two different table.
i have two table one is GPadvance and second one is GPexpenditure .
i have given advance to gp and get the advance report form the gpadvance. second is whatever the expenduture done by gp it saved in gpexpenditure table. now i want to use pivot table accordingly to GP wise like
you can see the first one data is coming from gpadvance and second column is amount spent data is coming is gpexpenduture.
i want to use single query to use both table data if gp has spent money or not,
iam using the following pivot table for the advance release
select * from
(
select Unit_Name,gpname,isnull(amount,0)as amount,Mcomponent
from AdvGP_report
where unit_id='7' and year='2014-15' and quarter='4'
) as s
pivot
(
sum(amount) for mcomponent in("1","2","3")
) as a
and second pivot query for amount spent is
(
select isnull(financialtarget,0)as FT,MinorComponent_Id
from GPWDPMPR
where unitid='7' and mpryear='2014-15'
) as i
pivot
(
sum(FT) for MinorComponent_Id in("19","4","8")
) as b
its giving the result but repeated many times the amount.
I have two tables:
table_people
col_name
col_sex
table_gender
col_male
col_female
Suppose the table_people consist three rows, (a,'M'),(b,'M'),(c,'F').
Now I need a query(subquery) to insert this first table value in second table as:
(a,c),(b,'').
If it is possible in mysql?
You table structure is bad.
My suggestion is to drop table_gender because it doesn't make sense at all. You already have a list of person with gender on table table_people.
You can generate a VIEW, if you want to list the gender separately.
CREATE VIEW MaleList
AS
SELECT col_name
FROM table_people
WHERE col_sex = 'M'
and another view for list of females only.
CREATE VIEW FemaleList
AS
SELECT col_name
FROM table_people
WHERE col_sex = 'F'
First of all the table structure is bad. I dont see a point of saving M or F in another table when you col_sex in your table_people. Still if you want to do it, first you have to specify a foreign key connecting the two tables.
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
I'm sure there are a ton of ways to do this, but right now I'm struggling to find the way that will work properly given the data.
I basically have a table containing duplicates which have additional fields tied to them and source details that take priority over others. So basically I added a "priority" field to my table which I then updated based on source priority. I now need to select the distinct records to populate my "unique" records table (which I'll then apply unique key constraint to prevent this from happening again on the field required!)....
So I have basically, something like this:
Select phone, carrier, src, priority
from dbo.mytable
So basically I need to pull distinct on phone in order of priority (1,2,3,4, etc), and basically pull the rest of the other data along with it and still keep UNIQUE on phone.
I've tried a few things using sub-select from the same table with min(priority) value, but outcome still doesn't seem to make sense. Any help would be greatly appreciated. Thanks!
EDIT I need to dedupe from the same table, but I can populate a new table with the uniques if needed based on my select statement to pull the uniques. This is in MSSQL, but figured anyone with SQL knowledge could answer.
For example, let's say I have the following rows:
5556667777, ATT, source1, 1
5556667777, ATT, source2, 2
5556667777, ATT, source3, 3
I need to pull uniques based on priority 1 first..... the problem is, I need to remove any all other dupes from the table based on the priority order without ending up with the same phone number twice again. Make sense?
So you're saying the combination (phone, priority) is unique in the existing table, and you want to select the rows for which the priority is smallest?
SELECT mytable.phone, mytable.carrier, mytable.src
FROM mytable
INNER JOIN (
SELECT phone, MIN(priority) AS minpriority
FROM mytable
GROUP BY phone
) AS minphone
ON mytable.phone = minphone.phone
AND mytable.priority = minphone.minpriority
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 :(