Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
SELECT tbl_post_upvote.id AS i ,
COUNT(user_id) AS c
FROM tbl_post_upvote
LEFT JOIN tbl_post ON (tbl_post.id=tbl_post_upvote.post_id)
WHERE tbl_post.author_id = 3 and is_seen = 0
UNION
SELECT tbl_post_downvote.id AS i,
COUNT(user_id)
FROM tbl_post_downvote
LEFT JOIN tbl_post ON (tbl_post.id=tbl_post_downvote.post_id)
WHERE tbl_post.author_id=3 and is_seen=0
$r_noti = mysql_query($sq_noti);
$l = mysql_fetch_assoc($r_noti);
$l_noti = mysql_num_rows($r_noti);
if ($l['c'] > 0 && $l['i']!=0) { ?>
<span class="badge">
<?= $l_noti; ?>
</span>
<?php } ?>
It displays result
i value 6 , 0
and c value 1 , 0 respectively.
i want to remove 0 ,0 row
and result is i value 6 and c value 1 only
You have to add GROUP BY i to your statements.
Please try the following:
select tbl_post_upvote.id as i ,count(user_id) as c
from tbl_post_upvote left join
tbl_post
on (tbl_post.id=tbl_post_upvote.post_id)
where tbl_post.author_id=3 and is_seen=0
group by i
union
select tbl_post_downvote.id as i,count(user_id)
from tbl_post_downvote left join
tbl_post
on (tbl_post.id=tbl_post_downvote.post_id)
where tbl_post.author_id=3 and is_seen=0
group by i
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is anyone know how to query this example table?
Images on imgur
I think it is a left join from the table branches to the table branch_operationals which I need to put where date inside the query.
Here are the examples:
TABLE branches
id
code
name
1
T2QD5
NewYork_Spot
2
MKGHB
London_Spot
3
IGHCZ
Miami_Spot
4
PJDSO
Tokyo_Spot
TABLE branch_operationals
id
branch_id
date
status
1
2
2020-12-05
closed
2
2
2020-12-06
closed
3
3
2020-12-06
open
4
2
2020-12-06
closed
5
2
2020-12-06
open
6
1
2020-12-16
closed
EXPECTED RESULT
id (from 'branches.id')
code
name
date
status
1
T2QD5
NewYork_Spot
2020-12-09
closed
2
MKGHB
London_Spot
2020-12-09
open
3
IGHCZ
Miami_Spot
2020-12-09
open
4
PJDSO
Tokyo_Spot
2020-12-09
null
And here is my current query:
select * from `branches` left join `branch_operationals` on `branches`.`id` = `branch_operationals`.`branch_id` where (`date` = 2020-12-21)
However, it returns null (empty data). But when I remove the where statement, it shows all data from the table branch_operationals with each data from table branches.
Currently I am using Laravel 8, and here is my Laravel syntax:
$branches = Branch::leftJoin('branch_operationals','branches.id','branch_operationals.branch_id')->where(function($q) use($request){
if($request->search){
$q->where(function($q) use($request){
$q->where('code','like','%'.$request->search.'%');
$q->orWhere('name','like','%'.$request->search.'%');
});
}
if($request->date_filter){
$q->where('date',$request->date_filter);
}else{
$q->where('date',\Carbon\Carbon::now()->toDateString());
}
})->get();
I need the query syntax or the Laravel Eloquent syntax.
I can provide you the query syntax which is normal LEFT JOIN.
The issue with your query is that you are doing INNER JOIN by applying the condition on the Left joined table in the WHERE clause. You can achieve the desired result as follows:
select * from
(select b.*, bo.*, -- use the needed column names with proper alias here. I have used *
row_number() over (partition by b.id order by bo.date desc) as rn
from branches b
left join branch_operationals bo on b.id = bo.branch_id and date <= 2020-12-21) t
where rn = 1
maybe it will work for you
select b.id ,b.code,b.name ,'2020-12-06' date ,o.status from branch b
left join ( select bo.id, bo.branch_id, bo.date, bo.status from
(select branch_id , max(id) id from branch_operationals where date='2020-12-06' group by branch_id ) as tmp
join branch_operationals bo on bo.id=tmp.id ) as o on b.id=o.branch_id
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am on a project of which requires Ranking and outputing the total score to users.
I have a table called userdata like this
id matricno mtstotal engtotal grandtotal Rank
1 MXX001 6 4
2 MXX002 9 8
3 MXX003 5 3
4 MXX004 4 7
5 MXX005 7 2
SELECT * FROM (
SELECT s.*, #rank := #rank + 1 rank FROM (
SELECT matricno, sum(mtstotal + engtotal) TotalPoints FROM userdata
WHERE class='1' AND subclass='A'
GROUP BY matricno
) s, (SELECT #rank := 0) init
ORDER BY TotalPoints DESC
) r
This SQL creates something temporary
matricno TotalPoints rank
MXX002 17 1
MXX004 11 2
MXX001 10 3
MXX005 9 4
MXX003 8 5
What I want is a statement added to the one above that will copy the TotalPoints and rank values and update the grandtotal and Rank columns respectively.
Even if it is formatting another code but it should be able to sum, rank and update in one single coding
When updating userdata join it with your query as subselect and use values from it
UPDATE userdata ud
JOIN(
SELECT s.*, #rank := #rank + 1 rank FROM (
SELECT matricno, sum(mtstotal + engtotal) TotalPoints FROM userdata
WHERE class='1' AND subclass='A'
GROUP BY matricno
) s, (SELECT #rank := 0) init
ORDER BY TotalPoints DESC
) r ON r.matricno = ud.matricno
SET ud.grandtotal = r.TotalPoints,
ud.Rank = r.rank
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am new in php. I have a problem. I want to fetch data from Mysql db and print it on my PHP page. I want to print latest record and some previous data of selected column. For example i have a two tables
table1( id, student_name )
table2: ( id, student_id, English, Math, Physics......., Total Marks, Obtain Marks and grade)
PROBLEM: I want that i can fetch whole new record but i want that i can also fetch previous grade of the student.
Can it is possible? How i Can do it?
With
SELECT * FROM (
SELECT * FROM table2 WHERE (student_id=2) ORDER BY id DESC LIMIT 2
) sub
ORDER BY id ASC
You should be able to select the two last rows.
Edit syntax formatting:
$result = mysql_query($sql);
$rows = mysql_fetch_assoc($result);
echo $rows[0]["grade"]; // new grade
echo $rows[1]["grade"]; // previous grade
You can try with the following:
$query = SELECT t1.*,t2.* FROM table1 as t1
LEFT JOIN table2 as t2 ON t1.id = t2.student_id
where t1.id = '1' ORDER BY t2.student_id desc LIMIT 2
$result = mysql_query($query);
$fetch_record = mysql_fetch_assoc($result);
$grade = $fetch_record[0]['pass-your-column-name'];
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
select
TOP 20 *
from
(select
TOP 30 *
from SavingRecurringMst_Kuber r, BhishiRegistrationMst b,
SavingRegistrationMst_Kuber s
where
RegID = FKRegID and
PkSavingRegID = FKSavinRegID ) as T
ORDER BY
Transactionate DESC
Try this...!
Pass the start index and end index as parameter from UI.
select SLNO, T.*
from (
select
ROW_NUMBER() OVER (ORDER BY Transactionate DESC) AS SLNO , SavingRecurringMst_Kuber.*
from SavingRecurringMst_Kuber r, BhishiRegistrationMst b,
SavingRegistrationMst_Kuber s
where
RegID = FKRegID and
PkSavingRegID = FKSavinRegID
) as T
where T.SLNO >= P_STARTINDEX and T.SLNO <= P_ENDINDEX
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I got the code to display a post by date from monday to friday but I thing the code display tha last post I publish
<?php
$query = mysqli_query($c, 'SELECT c.chapter, n.name, n.img FROM chapters AS c LEFT JOIN novels AS n ON c.novel = n.id WHERE c.time <= unix_timestamp()');
while($row = mysqli_fetch_row($query)):
?>
Can somebody tell me what code I have to use to display the publish post from monday to friday please?
I try with this, the server time is day 25 by I don't get a result and I least have display 1 post
$query = mysqli_query($c, 'SELECT c.chapter, n.name, n.img FROM chapters AS c LEFT JOIN novels AS n ON c.novel = n.id WHERE FROM_UNIXTIME(c.time,\'%N\')>=1 AND FROM_UNIXTIME(c.time,\'%N\')<=5');
Is fix now with this
$query = mysqli_query($c, 'SELECT c.chapter, n.name, n.img FROM chapters AS c LEFT JOIN novels AS n ON c.novel = n.id WHERE DAYOFWEEK(FROM_UNIXTIME(c.time))>=1 AND DAYOFWEEK(FROM_UNIXTIME(c.time))<=5');
while($row = mysqli_fetch_row($query)):
?>
You can get day name by this
SELECT DAYNAME(CURDATE()), DAYNAME(DATE_ADD(CURDATE(), INTERVAL 4 DAY));
OR
SELECT DAYNAME('2013-11-25'), DAYNAME(DATE_ADD('2013-11-25', INTERVAL 4 DAY));
Hope it should help for you.