I have data set with full of data...
Id | loc |SubLoc |beds
1 15 5803 1
2 15 5803 1
3 16 2813 1
4 16 2813 2
5 16 2830 1
6 20 2020 4
7 19 2513 3
I have this php/mysql code to traverse it
select id,loc,SubLoc,beds from table where id != 144 AND price > 140000 AND price < 210000 AND category_id=1
while($rw = mysql_fetch_array($rs2)){}
Now i want to sort $rw in this manner
I have three values
loc=16,SubLoc=2813,beds=1
I want to show same result but show all results in this manner
1- having loc==16 on top then
2- having SubLoc = 2813 then
3- having beds = 1
and then rest of results
How this can be done in php?
UPDATED
I want to get these Ids
3 as it matches 3 parameters
4 as it matches 2 parameters
5 as it matches 2 parametrs
1 as it matches 1 parameter
and so on
Try that:
select id,loc,SubLoc,beds from table1
where loc = 16 or subloc = 2813 or beds =1
order by
case when loc = 16 then 0
when subloc = 2813 then 1
when beds = 1 then 2 end asc
DEMO
to get other results also without matching then use this:
select id,loc,SubLoc,beds from table1
order by
case when loc = 16 then 0
when subloc = 2813 then 1
when beds = 1 then 2
else 3 end asc
DEMO
EDIT: to include all cases use this:
select id,loc,SubLoc,beds from table1
order by
case when loc = 16 and subloc=2013 and beds = 1 then 0
when loc = 16 and subloc = 2813 then 1
when loc = 16 and beds = 1 then 2
when subloc = 2813 and beds = 1 then 3
when loc = 16 then 4
when subloc = 2813 then 5
when beds = 1 then 6
else 7 end asc
Try with order
ORDER BY SubLoc=16 DESC,loc, beds
Related
I have built a live scoring system for a game. When the scores are entered into the MySQL database, they are entered like this...
ID userid sessionid sprintid pointvalue1 pointvalue2 pointvalue3
1 1 1 1 10 5 2
2 2 1 1 10 5 3
3 3 1 1 12 6 3
4 1 1 2 10 6 4
5 2 1 2 12 5 3
6 3 1 2 9 4 3
As you can see from the above, there is 1 session, 2 different sprints (iterations) and 3 different users. I want to display a leader board. The leader board would show (by iteration), user name (from another table), sprint, point value 1, point value 2, point value 3, sum of point value 1 cumulative for all iterations in that session. So on iteration 1, point value 1 and sum of point value 1 cumulative would be the same. But, in iteration 2, I should see a sum of point value 1 of 20 for user 1, 22 for user 2, and 21 for user 3, in the proper order (descending). I tried a subquery using a select sum, but couldn't quite get it right, and I tried a SUM(IF()), which seemed like it would be right, but it's not. I guess I need some guidance on which direction to go.
I think this query will get you the raw data you want. You will probably want to adjust the query with some form of ordering on total points, and possibly WHERE and LIMIT clauses to meet your exact needs.
SELECT s1.userid,
s1.sessionid,
s1.sprintid,
s1.pointvalue1,
s1.pointvalue2,
s1.pointvalue3,
SUM(s2.pointvalue1) AS sum_pointvalue1,
SUM(s2.pointvalue2) AS sum_pointvalue2,
SUM(s2.pointvalue3) AS sum_pointvalue3
FROM scores s1
JOIN (SELECT userid,
sessionid,
sprintid,
SUM(pointvalue1) AS pointvalue1,
SUM(pointvalue2) AS pointvalue2,
SUM(pointvalue3) AS pointvalue3
FROM scores
GROUP BY userid, sessionid, sprintid
) s2
ON s2.userid = s1.userid AND s2.sessionid = s1.sessionid AND s2.sprintid <= s1.sprintid
GROUP BY userid, sessionid, sprintid
ORDER BY sprintid
Output:
userid sessionid sprintid pointvalue1 pointvalue2 pointvalue3 sum_pointvalue1 sum_pointvalue2 sum_pointvalue3
2 1 1 10 5 3 10 5 3
3 1 1 12 6 3 12 6 3
1 1 1 10 5 2 10 5 2
2 1 2 12 5 3 22 10 6
3 1 2 9 4 3 21 10 6
1 1 2 10 6 4 20 11 6
SQLFiddle Demo
i have a database table in mySQL like this and want create tree results with current value child.
id item_id secondary_id
1 1 1
2 1 1
3 1 1
4 1 3
5 1 3
6 1 3
7 1 3
8 1 3
9 1 3
10 1 3
11 1 3
12 1 1
13 1 1
14 1 1
15 1 1
16 1 7
17 1 7
18 1 7
19 1 7
20 1 7
I want get final result like this:
$current_value = 18;
-> Root with id = 1
---> Reply with id = 2
---> Reply with id = 3
-----> Reply with id = 6
-----> Reply with id = 7
-------> Reply with id = 16
-------> Reply with id = 17
-------> Reply with id = 18 <-- this target $current_value
-------> Reply with id = 19
-------> Reply with id = 20
-----> Reply with id = 8
-----> Reply with id = 9
---> Reply with id = 12
---> Reply with id = 13
I use this SQL query, but only get result current target, previous value and next value from target.
$query = "(
SELECT *
FROM (
SELECT *
FROM table_name
WHERE id < $current_target
ORDER BY id
LIMIT 2
) AS comment
ORDER BY comment.id ASC
)
UNION ALL
(
SELECT *
FROM (
SELECT *
FROM table_name
WHERE id = $current_target
ORDER BY id DESC
LIMIT 2
) AS comment
ORDER BY comment.id ASC
)
UNION ALL
(
SELECT *
FROM (
SELECT *
FROM table_name
WHERE id > $current_target
ORDER BY id
LIMIT 2
) AS comment
ORDER BY comment.id
)";
I want get result up to Root.
Below are the 2 tables:
classes
academic_year_id student_id standard
2 1 10
2 2 10
2 3 10
2 4 10
2 5 10
2 6 10
2 7 11 Science
2 8 11 Science
Here standard is not INT
student
student_id roll_no name teacher_approval
1 0 S Sumeet G 1
2 0 Nair Nikhil R 1
3 0 Nayak Ankita R 0
4 0 Rathod Ketan P 0
5 0 Patel Vishal D 1
6 0 Patel Jignesh R 0
7 0 Prajapati Bhavesh A 1
8 0 Shah Harsh N 1
What i want to do:
when teacher selects standard -> 10 and press a button "Assign Roll No"
i want all student of 10th standard to be given roll no sequentially order by name and which are approved by teacher that is teacher_approval = '1'
So my student table becomes as below:
student
student_id roll_no name teacher_approval
1 3 S Sumeet G 1
2 1 Nair Nikhil R 1
3 0 Nayak Ankita R 0
4 0 Rathod Ketan P 0
5 2 Patel Vishal D 1
6 0 Patel Jignesh R 0
7 0 Prajapati Bhavesh A 1
8 0 Shah Harsh N 1
I have so far tried below code:
$standard = $_POST['standard']
SET #incr = 0
UPDATE
student AS st
JOIN
( SELECT * FROM
classes AS classes
WHERE standard = '".$standard."'
) AS tmp
ON tmp.student_id = st.id
SET
st.roll_no = #incr:=#incr+1
WHERE st.teacher_approval = '1'
ORDER BY st.name ASC
But it gives me an error: incorrect usage of update and order by
Anyone with a similar experience? Any help would be appreciated. Thanks.
In MySql, you can't have an ORDER BY as part of the UPDATE directly when using multiple tables (see this link). Try this instead :
UPDATE student AS table1
INNER JOIN (
SELECT st.student_id, st.roll_no, st.name, st.teacher_approval
FROM student AS st
JOIN (
SELECT * FROM
classes AS cl
WHERE cl.standard = '".$standard."'
) AS tmp
ON tmp.student_id = st.student_id
WHERE st.teacher_approval = '1'
ORDER BY st.name ASC
) AS table2
ON table2.student_id = table1.student_id
SET table1.roll_no = #incr:=#incr+1
I have the folowing query that shows my products.
SELECT DISTINCT productsmap.id, attributes_product.waarde,
attributes_product.att_id, productsmap.name, productsmap.category
AS categoryId, brand.naam AS brand, productsmap.sku, categorie.name AS
category, productsmap.price, productsmap.shops
FROM productsmap
INNER JOIN categorie ON productsmap.category = categorie.id
INNER JOIN brand ON productsmap.brand = brand.id
INNER JOIN attributes_product ON productsmap.id = attributes_product.pmapid
WHERE productsmap.category
IN ( 2, 3, 4, 5, 6, 7, 8 )
AND productsmap.shops >0
AND (
productsmap.price >= 3.94
AND productsmap.price <= 204.99
)
AND productsmap.online =1
LIMIT 0 , 30
It gives the folowing results I cropped a bit:
(id) (waarde) (att_id) (name) (categoryId) (brand) (sku)
2 109 1 Name 4 Merk 70000
2 2013 2 Name etc etc etc
2 123 3 Name etc etc etc
3 103 1 Name2 etc etc
3 2012 2 Name2
3 123 3 Name2
4 110 1 3name
4 2013 2 3name
4 102 3 3name
Whit multiple times the same id and name only diffrent att_id and waarde. But i need to add to my query (attributes_product.att_id = 1 and waarde = 109) and (attributes_product.att_id = 2 and waarde = 2013)
But this is not possible because it are diffrent results. How can I solve this issue? Or on what can i search to solve this issue?
It attributes.product are att_id and waarde
TABLE PRODUCTS_CATEGORIES
product_id category_id link_type position
2 22 M 0
3 22 M 0
4 22 M 0
5 22 M 0
6 1 M 0
7 1 M 0
8 1 M 0
9 1 M 0
10 1 M 0
11 1 M 0
TABLE PRODUCT_PRICES
product_id price lower_limit usergroup_id
2 39.99 1 0
3 69.99 1 0
4 99.99 1 0
5 124.99 1 0
6 169.99 1 0
7 199.99 1 0
8 249.99 1 0
9 24.99 1 0
10 29.99 1 0
11 34.99 1 0
I want to be able to grab the lowest products price from the category - the function i have currently made is:
function fn_get_category_min_price($category_id)
{
if (!empty($category_id))
{
$product_min_price = db_get_field("SELECT product_id FROM ?:products_categories WHERE category_id = ?i", $category_id);
$category_min_price = db_get_field("SELECT MIN(price) FROM ?:product_prices WHERE product_id = ?i", $product_min_price);
if (!empty($category_min_price))
{
return $category_min_price;
}
else
{
return "";
}
}
return false;
}
but it is not 100% working, although it grabs the price from the right category_id - it does not appear to be grabbing the lowest all the time.... anyone have any ideas or a better way of writing those mysql queries??
As far as I understand, your code takes first product from specified category and then just returns it's price. Sometimes it's minimum, sometimes not.
This can be done with single SQL query:
select min(price) from product_prices p, product_categories c where p.product_id=c.product_id and c.category_id = $category_id
You can do this with an single SQL query by joining the two tables (Natural join):
Select Min(`prize`)
from product_categories natural join product_prices
where category_id = ?i