I want to show only that data which has date is null - php

I want to show only that data which date is null
Here is my query:
$Query = "SELECT att.`EmployeeCode`,emp.FullName ,emp.EmployeeID,emp.Photo,sum(TIME_TO_SEC(att.FinalTime)/60 - IFNULL(sp.TotalTimeLateMark,$TotalTimeLateMark)) OverTime
FROM `hr_tblattendancehistory` att
left join hr_tblemployee emp on att.EmployeeCode = emp.CardID
left join hr_tblspecialattendancesetting sp on sp.Date = att.Date
WHERE emp.FullName!='' and att.`Date` Between '$FromDate' and '$Todate=''' and (TIME_TO_SEC(att.FinalTime)/60 -IFNULL(sp.TotalTimeLateMark,$TotalTimeLateMark)) > 0
group by att.`EmployeeCode`,emp.Photo,emp.FullName
order by SUM(TIME_TO_SEC(att.FinalTime)/60 -IFNULL(sp.TotalTimeLateMark,$TotalTimeLateMark)) DESC";
How can I do this ?

You can use IS NULL
e.g.
SELECT * FROM YourTable WHERE DateColumn IS NULL;

In your particular case you are probably looking for IS NULL, just like Nilesh Thakkar has pointed out. But in the HAVING clause:
SELECT * FROM YourTable A
LEFT JOIN AnotherTable B on A.DateColumn=B.DateColumn
HAVING DateColumn IS NULL;
That is because the selection should happen after the join, not before.

Related

passing results of a query into another query

I need to run a query like below
SELECT `loginname`, `id`, `locality`, `wherefrom`
FROM `Volley` JOIN `register` ON loginname=username
WHERE CONCAT_WS('', wherefrom,locality) LIKE '%$search%'
from this result i need to take id and pass it to the other query to check that id exist in another table like below.
SELECT IF(EXISTS(SELECT *
FROM `likes`
WHERE Volleyid = '20'),1,0)
Can i combine this into a single query? For the first one i may get more that 1 rows. I tried and got the results of first query into a array but i am not able to get that particular value from array.
$data = array();
for ($x = 0; $x < mysqli_num_rows($query1); $x++)
{
$data[] = mysqli_fetch_assoc($query1);
}
SELECT IF(EXISTS(SELECT * From
(
SELECT `loginname`,
`id`,
`locality`,
`wherefrom`,
Volleyid
FROM `Volley`
JOIN `register`
ON loginname=username
WHERE CONCAT_WS('', wherefrom,locality) LIKE '%$search%'
) AS T1
WHERE Volleyid = '20'),1,0)
You can use a subquery with IN clause (i think what you what it ś that Volleyid it's equal to id):
SELECT IF(EXISTS(SELECT *
FROM `likes`
WHERE Volleyid IN
(SELECT `id`
FROM `Volley` JOIN `register` ON loginname=username
WHERE CONCAT_WS('', wherefrom,locality) LIKE '%$search%')),1,0)
If you want to get loginname, id, locality, wherefrom columns in the first query alongside with the 1 or 0 flag checked against table likes in single result set. You can write query that looks like this:
SELECT v.*, IF(l.Volleyid IS NOT NULL, 1, 0) as existFlag
FROM
(SELECT `loginname`, `id`, `locality`, `wherefrom`
FROM `Volley`
JOIN `register` ON loginname=username
WHERE CONCAT_WS('', wherefrom,locality) LIKE '%$search%') AS v
LEFT JOIN
(SELECT DISTINCT Volleyid
FROM `likes`) l ON v.id = l.Volleyid;

Left join with multiple where clause for the first (left table)

I got this Query:
$query = mysql_query("SELECT arbejdsopgave.*,
DATE_FORMAT(dato, '%d-%m-%Y') AS tid,
tilfoejelser.*,
DATE_FORMAT(datotf, '%d-%m-%Y') AS tid2
FROM arbejdsopgave LEFT JOIN tilfoejelser
ON arbejdsopgave.sub_id=tilfoejelser.sub_id2
WHERE arbejdsopgave.cat_id = '$id'
AND datotf IS NULL
OR datotf = (select max(datotf)
FROM tilfoejelser
WHERE arbejdsopgave.sub_id=tilfoejelser.sub_id2)
ORDER BY arbejdsopgave.sub_id")
or die(mysql_error());
I need to to only show rows from the table "arbejdsopgave" where arbejdsopgave.cat_id = '$id' but also where arbejdsopgave.status = 'closed'
I tried both in the join on and in the where.
First I get everything despite the extra where. Secondly I get only the rows where there are a join.
Can anyone help me - I am amazed that i even made it this far looking at my Query... Pls help
This should work:
SELECT arbejdsopgave.*, DATE_FORMAT(dato, '%d-%m-%Y') AS tid, tilfoejelser.*, DATE_FORMAT(datotf, '%d-%m-%Y') AS tid2
FROM arbejdsopgave
LEFT JOIN tilfoejelser ON arbejdsopgave.sub_id=tilfoejelser.sub_id2
WHERE arbejdsopgave.cat_id = '$id'
AND arbejdsopgave.status = 'closed'
AND (datotf IS NULL
OR datotf = (
select max(datotf) FROM tilfoejelser
WHERE arbejdsopgave.sub_id=tilfoejelser.sub_id2) )
ORDER BY arbejdsopgave.sub_id
I suppose you forget about () around datotf OR condition

compare rows of two query set result in same table

I have table where I store data on basis of date.
Now I need to check that: is there any difference between data of rows with two different dates.
In a simple way you can say that I have two queries which select data from same table now I have to compare each row and column value. for example my two query are -
SELECT * FROM `national` WHERE `upload_date` = '2015-08-04' // return 106 rows
and
SELECT * FROM `national` WHERE `upload_date` = '2015-08-01' // return 106 rows
I have tried to compare this with below query but the result not seem to be correct to me, I am not satisfy with this.
Select U.* from
(SELECT t1.* FROM `national` t1 where upload_date = '2015-08-01'
union all
SELECT t2.* FROM `national` t2 WHERE `upload_date` = '2015-08-04' ) U
GROUP BY emp_id, uqi_id
HAVING COUNT(*) = 1
Can Any one please provide me correct query ?? thank you
Try this
(
SELECT t1.*
FROM
`national` t1, `national` t2
where
t1.upload_date = '2015-08-01' and t2.upload_date='2015-08-04' and
-- put your columns here that you want to compare for same DATA
-- like t1.name=t2.name and etc...
)
You can try with something like that
SELECT * FROM (SELECT * FROM `national` WHERE upload_date = '2015-08-01') a
INNER JOIN (SELECT * FROM `national` WHERE upload_date = '2015-08-04') b
ON a.emp_id = b.emp_id AND a.uqi_id = b.uqi_id
ORDER BY uqi_id

Select with inner join

i need select some data from two tables ,
please help me use inner join for this selection .
players in selction2 must not be in selection1...
first select :
$rs = "SELECT *
FROM `player`
WHERE `status`=1 AND `credit`>=1 AND `username` NOT LIKE '$user'
ORDER BY ls ASC,credit DESC
LIMIT 0 ,10;
Second: this players must remove from result of selection1
$rs2 = "SELECT *
FROM `ip_log`
WHERE `playerid`='$ui' AND `win`='1' AND `date`='$date' ";`
You can use LEFT JOIN for this:
This shows the log messages for everyone not in selection 1.
SELECT l.*
FROM ip_log AS l
LEFT JOIN
(SELECT username
FROM player
WHERE status = 1 AND credit >= 1 AND username NOT LIKE '$user'
ORDER BY ls ASC, credit DESC
LIMIT 10) AS p
ON l.player = p.username
WHERE win = 1 and date = '$date'
AND p.username IS NULL
This shows the top 10 player data, except the ones with log messages in selection 2
SELECT p.*
FROM player AS p
LEFT JOIN ip_log AS l ON l.player = p.username AND l.win = 1 AND l.date = '$date'
WHERE p.status = 1 AND p.credit >= 1 AND p.username NOT LIKE '$user'
AND l.player IS NULL
ORDER BY p.ls ASC, p.credit DESC
LIMIT 10
In both cases, testing a column in the second table with IS NULL makes it return only the rows in the first table that don't have a match in the second table. See
Return row only if value doesn't exist
You can do it with LEFT JOIN
SELECT player.*,ip_log.* FROM `player` LEFT JOIN `ip_log` ON player.id!=ip_log.playerid GROUP BY player.id

Conditionally order by another column

I query a database and order by a date column in the table and also by another column in another table. This is working perfectly.
SELECT * FROM myTable1 LEFT JOIN myTable2 b ON myTable1.id = b.id WHERE myTable1.id !='foo' ORDER BY myTable1.dateColumn DESC, b.column2 ASC
What I would like is to do is ORDER by myTable1.dateColumn ONLY if there is no value in b.column2 for that particular record. As you can see from my above current implementation, it will always ORDER BY myTable1.dateColumn regardless.
Stackoverflow Suggested tag for this question is mysql but in fact I am using mysqli. It makes no difference for the purpose of this question.
You should better provide some data sample and expected result, but so far you can try:
SELECT * FROM myTable1
LEFT JOIN myTable2 b
ON myTable1.id = b.id
WHERE myTable1.id !='foo'
ORDER BY CASE WHEN b.column2 IS NULL myTable1.dateColumn ELSE NULL END DESC, b.column2 ASC
You can put CASE Statements inside of an ORDER BY
SELECT *
FROM myTable1
LEFT JOIN myTable2 b
ON myTable1.id = b.id
WHERE myTable1.id != 'foo'
ORDER BY CASE WHEN b.column2 IS NULL THEN 1 ELSE 0 END ASC, myTable1.dateColumn DESC
You can use a function in the order part; something like the following:
SELECT * FROM myTable1 LEFT JOIN myTable2 b ON myTable1.id = b.id WHERE myTable1.id !='foo'
ORDER BY IF(b.column2 IS NULL,1,0),IF(b.column2 IS NULL,myTable1.dateColumn,NULL) DESC, b.column2 ASC

Categories