select multiple rows on nested condition in mysql database - mysql

select multiple rows on nested condition and nested limit in mysql database
Actually i have one table like:
id name levels
1 Red 1
2 Red Light 1
3 Green 2
4 Green Light 2
5 Blue 3
6 Blue Light 3
7 Blue Dark 3
I want fetch any rows according to levels with any limits Like:
From Level-1 = 1 row
From Level-2 = 2 row
From Level-3 = 2 row
total rows are 5 according to levels.
Any one know about single query is possible?
or
three foreach loop will be implement?

Related

generate a pivot table out of mysql data with php

I am having problems to figure out, how to make a pivot table with php/mysql.
There is no Code now, its mor to understand how it works.
Lets say i have a Table with packages
Table packages
pack_ID pack_Name
1 ABC
2 XYZ
3 DEF
and so on
Then a Table with Items which are part of the packages
Table items
item_ID item_Name
1 red item
2 blue item
3 green item
4 black item
And a Table to mix them
Table item-pack
pack_ID item_ID
1 2
1 4
2 1
2 3
3 2
3 4
3 3
3 1
And the result should be a pivot table
item/ pack
ABC XYZ DEF
red Item x x
blue Item x x
green Item x x
black Item x x
First i would QUery the Packages with:
SELECT p.pack_ID, p.pack_Name FROM packages
Which should give me and array with the packages.
Then i would run a for each or while and query the items like this
SELECT i.item_ID, i.item_Name FROM item-pack ip
JOIN item i ON i.item_ID = ip.item_ID
WHERE ip.pack_ID = $ValueFromThe QUery
But how the heck do i retun the result as a pivot?
is this so dificult with php?
Thy for any help.

aggregate of the group columns along with the group in a table

I am very new to Microsoft reporting. I have the following table in my database:
CategoryName Id
Normal 1
High 2
Normal 3
Low 4
Normal 5
Normal 6
Normal 7
Normal 8
Low 9
Low 10
Low 11
High 12
I want to group by Category and also show the count of each category. Here is what I did:
I inserted a two column table and I grouped by the categoryName in the first column and in the second column, I tried doing
=CountDistinct(Fields!CategoryName.Value)
This is what I am seeing in the report
High 1
1
Normal 1
1
1
1
1
1
1
Low 1
1
1
1
I want to see something like this:
Category Count
Normal 6
Low 4
High 2
any help will be highly appreciated
Delete the Detail row and put the Count epxression in the Group row.
You'd probably be better off doing this in the query. Easier and leaves less room for error. Something like the following should work.
SELECT categoryName, COUNT(*)
FROM your table
GROUP BY categoryName

Filtering duplicate rows based on multiple columns (QueryBuilder 4.2)

I've ran into a little difficulty when trying to filter top N results for a table.
Assume the following table:
ID, X, Y, Result0, Result1
-------------------------------
0 0 0 1 4
1 0 1 2 5
2 0 1 1 4
3 0 2 2 5
4 0 3 0 1
5 1 3 3 4
6 1 3 2 5
7 1 3 4 6
So, let's say I want to get the top 2 results for the highest Result0 value, using Result1 as a tie breaker if the Result0 values are equal, and having only distinct values for (X,Y),
if I'll run the following query:
$result = DB::table('table')
->orderBy('Result1', 'DSC')
->orderBy('Result0', 'DSC')
->take(300)
->get();
This code will return IDs 5,7, because they have the highest Result0 values, but the X,Y for these fields are identical, and I'd like to get only top result for distinct X,Y values.
I tried adding a
->groupBy('X','Y')
But it grouped the entries based on the database order of the entries (i.e the ID) rather than my sorting of that table.
Anyone has any idea how can I achieve my goal?

group by based on two columns with same but vice versa values

What I Have:
I have a table where I have two basic columns on which the query is supposed to work
c1|c2
1 2
2 1
1 3
3 1
2 4
Now I want the result after the execution of the query is this
c1|c2
2 1
3 1
Notice
(3,1) was the last occurence of 1 and 3 no matter in which order they are as along as it is 1 and 3 and same is (2,1)
How can I achieve this where one value of the two columns is static for example in this case (1) is static and I want only those rows which has (1) in any of the two columns in their last occurrence.. Can any one help ?

query the same field multiple times in the same query

I need to filter a table in mysql but can't get past the beginning.
The table has 2 fields:
ID_house house_feature
1 1
1 2
1 4
1 5
2 1
2 3
2 4
3 1
3 2
3 3
I need to filter this table using the following parameters:
house feature = 1
AND
house feature = 2
AND
house feature = 3
So that I get all houses with the requested feature.
I already tried to create something similar to this:
SELECT *
FROM houses
WHERE
house_feature = 1
AND
house_feature = 2
AND
house_feature = 3
But it doesn't work as I expected.
Is there a way to get this result with MySQL?
It seems that I acn filter the table using only the OR operator but this way I can't get the right result.
Thanks in advance for any help.
tony
You can do so ,by matching the distinct count of features per house ,so the house with exactly these 3 features will be returned
SELECT *
FROM t
WHERE
house_feature IN(1 ,2,3)
group by ID_house
having count(distinct house_feature) = 3
Demo