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.
Related
Before you misread the title... I'm not asking how to echo the number of rows, I'm simply looking to find the position that the row is in.
Let's say we have five total rows:
+---------------------+
| Post ID | Author |
+---------------------+
| 463 | Me |
| 477 | Me |
| 718 | Me |
| 883 | Me |
| 276 | Me |
+---------------------+
Now I'd like to get the position of the row that contains the post ID of 718. From looking at the data, we can visually see that it is the third row in the data. How can I echo this in PHP?
when you fetch records, you may use a variable as position number.
#DB is a class connect to mysql database.
DB::init();
$sql = "SELECT * FROM RowNo ";
$stmt = DB::query( $sql );
$i = 1;
while( $rec1 = $stmt->fetch() ) {
echo "row $i : ";
print_r( $rec1 );
echo '<BR>'.PHP_EOL;
$i++;
};
result :
row 1 : Array ( [Post ID] => 8788277463 [Author] => Me )
row 2 : Array ( [Post ID] => 2894728477 [Author] => Me )
row 3 : Array ( [Post ID] => 3898994718 [Author] => Me )
row 4 : Array ( [Post ID] => 4891784883 [Author] => Me )
row 5 : Array ( [Post ID] => 1185819276 [Author] => Me )
You may use row_number :
#1. ORDER BY Post ID
SELECT * , row_number() over ( order by Post ID ) FROM RowNo order by Post ID asc
Post ID Author row_number() over ( order by `Post ID` )
1185819276 Me 1
2894728477 Me 2
3898994718 Me 3
4891784883 Me 4
8788277463 Me 5
#2. ORDER BY Post ID DESC
SELECT * , row_number() over ( order by Post ID desc ) FROM RowNo order by Post ID desc
Post ID Ascending 1 Author row_number() over ( order by `Post ID` desc )
8788277463 Me 1
4891784883 Me 2
3898994718 Me 3
2894728477 Me 4
1185819276 Me 5
#3. ORDER BY part of Post ID
SELECT * , substr(Post ID, 4, 2), row_number() over ( order by substr(Post ID, 4, 2) ) FROM RowNo order by substr(Post ID, 4, 2) asc
Post ID Author substr(`Post ID`, 4, 2) Ascending 1 row_number() over ( order by substr(`Post ID`, 4, 2) )
4891784883 Me 17 1
2894728477 Me 47 2
1185819276 Me 58 3
8788277463 Me 82 4
3898994718 Me 89 5
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 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
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