How can I display the values excluded by the Where clause? - mysql

I'm trying to figure out how to to display all those values that are excluded from this WHERE clause instead of those that are chosen by:
$pdo = $service->pdo;
$sql = "SELECT a.id, a.name, a.surname, a.job, b.start_date, b.end_date
FROM table1 a
INNER JOIN table2 b on b.id = a.id
WHERE a.job = '{$job}'
AND
STR_TO_DATE(b.`start_date`, '%d-%m-%Y') = '{$date}'
GROUP BY a.id ";
return $pdo->query($sql)->fetchAll(PDO::FETCH_OBJ);
What this query returns are values that match "work title" first and then values that exist in that date in the table 2. What I'm trying to show are all the other values based on the "work title" excluding those values that exist for that specific date. Basically I need to figure out for example how to know who is free in a specific date based on this query.
Thank you for your help

You can modify your query and set the inverse where clause like this, you get the rows that don't match 'work title' OR (this is important) the rows that don't match the date requeriment in table2:
$pdo = $service->pdo;
$sql = "SELECT a.id, a.name, a.surname, a.job, b.start_date, b.end_date
FROM table1 a
INNER JOIN table2 b on b.id = a.id
WHERE a.job <> '{$job}'
OR
STR_TO_DATE(b.`start_date`, '%d-%m-%Y') <> '{$date}'
GROUP BY a.id ";
return $pdo->query($sql)->fetchAll(PDO::FETCH_OBJ);
EDIT
After reading your comments, i see that you donĀ“t need the roes that your query has dircarded, you need the rows that match the first requeriment and don't match the second one. So you would need it this way:
$pdo = $service->pdo;
$sql = "SELECT a.id, a.name, a.surname, a.job, b.start_date, b.end_date
FROM table1 a
INNER JOIN table2 b on b.id = a.id
WHERE a.job = '{$job}'
AND
STR_TO_DATE(b.`start_date`, '%d-%m-%Y') <> '{$date}'
GROUP BY a.id ";
return $pdo->query($sql)->fetchAll(PDO::FETCH_OBJ);

Related

SELECT statement inner join

I'm trying to find out if I can make one query to out of three I have now.
$sql = "SELECT *
FROM joblist WHERE customer = :customer"; // this is a bindParam
$sql_val = "SELECT a.customer, SUM(a.value) AS tvalue
FROM joblist AS a
INNER JOIN joblist AS b
ON a.customer = b.customer
GROUP BY a.customer";
$sql_bal = "SELECT *
FROM (SELECT SUM(balance) AS tbalance
FROM joblist GROUP BY customer) AS total_balance
WHERE tbalance = :tbalance";
The table that I am using is customer and following is are the columns.
ID - work_order - customer - description - value - balance - status - notes
I'm using php PDO and HTMl of course.
Basically, I have a search function used to query a company. The result are displayed, but I need the totals for the value and balance columns. The only thing that really works is pulling in the data from the table. I've had no success at getting the totals for value and balance.
Current code page
May not understand your question well so I'll make some guess:
SELECT
t1.*,
t2.tvalue,
t3.tbalance
FROM
(SELECT
*
FROM
joblist
WHERE customer = :customer) t1
JOIN
(SELECT
a.customer,
SUM(a.value) AS tvalue
FROM
joblist AS a
GROUP BY a.customer) t2
ON t1.customer = t2.customer
JOIN
(SELECT
b.customer,
SUM(b.balance) AS tbalance
FROM
joblist AS b
GROUP BY b.customer
HAVING tbalance = :tbalance) t3
ON t3.customer = t1.customer;

Select from my tabel where date = specific date

My date in table is TIMESTAMP something like this 2016-05-28 18:39:00
Now I am trying to select from table where date = 2016-05-28
here is my query
$getReport = $db->prepare('SELECT a.proId, a.userId, DATE(a.date), a.status, a.canceledBy, b.id, b.pName, b.pPrice FROM purchaseshistory AS a INNER JOIN products AS b ON(a.proId=b.id) WHERE a.date=?');
$getReport->bind_param('i', $dateOfBirth);
$getReport->execute();
$getReport->bind_result($proId, $userId, $date, $status, $canceledBy, $id, $pName, $pPrice);
$getReport->store_result();
while ($getReport->fetch()) {
Date is coming from here
$dateOfBirth = $_POST['dateOfBirth'];
results are empty
could be you need a correct date format
SELECT a.proId, a.userId,
DATE(a.date), a.status, a.canceledBy, b.id, b.pName, b.pPrice
FROM purchaseshistory AS a
INNER JOIN products AS b ON(a.proId=b.id)
WHERE date_format(a.date,'%Y-%m-%d') = ?
You've converted a.date to a DATE in the SELECT, but not in the WHERE clause, I would recommend modifying your query as below:
SELECT
ph.proId, ph.userId, DATE(ph.date), ph.status, ph.canceledBy,
p.id, p.pName, p.pPrice
FROM purchaseshistory AS ph
INNER JOIN products AS p ON ph.proId = p.id
WHERE DATE(ph.date) = ?

Trying to add one last SUM() column to my query in SQL Server 2008

I have the first query which is producing correct results. What I need is I need to add the sum of values as a last column grouped by surveyid. I can't insert Sum(c.value) into the first query because it is an aggregate function. I have the correct query as my second query below. I know there's pivot functionality but not sure if it can be used here. I do realize that there will be repetition but that's okay.
'first query
SELECT
A.PATIENTID, B.STUDENTNUMBER, c.surveyid,
convert(varchar, A.CreatedDate, 107),
C.QuestionID, C.Value, D.Question
FROM
dbo.Survey A, dbo.Patient B, [dbo].[SurveyQuestionAnswer] C, [dbo].[LookupQuestions] D
WHERE
A.PATIENTID = B.ID
and c.SurveyID = A.ID
and c.QuestionID = d.ID
and c.questionid <> 10
ORDER BY
A.PATIENTID
'second query
select
c.surveyid,SUM(c.value) as scores
from
dbo.SurveyQuestionAnswer c
group by
c.SurveyID
order by
SurveyID '---not important
You can use SUM if you add the OVER clause. In this case:
SELECT
A.PATIENTID, B.STUDENTNUMBER, c.surveyid,
convert(varchar, A.CreatedDate, 107),
C.QuestionID, C.Value, D.Question,
SUM(c.Value) OVER(PARTITION BY c.surveyid) scores
FROM
dbo.Survey A
INNER JOIN dbo.Patient B
ON A.PATIENTID = B.ID
INNER JOIN [dbo].[SurveyQuestionAnswer] C
ON c.SurveyID = A.ID
INNER JOIN [dbo].[LookupQuestions] D
ON c.QuestionID = d.ID
WHERE
c.questionid <> 10
ORDER BY
A.PATIENTID
You could use something like this:
SELECT
s.PATIENTID, p.STUDENTNUMBER, sqa.surveyid,
CONVERT(varchar, s.CreatedDate, 107),
sqa.QuestionID, sqa.Value, lq.Question,
Scores = (SELECT SUM(Value) FROM dbo.SurveyQuestionAnswer s2 WHERE s2.SurveyID = s.ID)
FROM
dbo.Survey s
INNER JOIN
dbo.Patient p ON s.PatientID = p.ID
INNER JOIN
[dbo].[SurveyQuestionAnswer] sqa ON sqa.SurveyID = s.ID
INNER JOIN
[dbo].[LookupQuestions] lq ON sqa.QuestionID = lq.ID
WHERE
sqa.questionid <> 10
ORDER BY
s.PATIENTID
By having a subquery with the SUM(...) you should be able to get that sum as a single value and you don't need to use any grouping function

insert data into table that results from a JOIN SELECT QUERY?

How can I insert data to a table that results from a JOIN SELECT query
I'd like to insert into b.URL the value ="ok" from the result of the query below
SELECT
a.ESN,
b.URL,
a.Status,
a.GroupID,
a.RouteID
FROM STx a
LEFT JOIN Routes b
ON a.RouteID = b.RouteID
WHERE a.GroupID = 39
AND a.Status = "Tested"
order by a.ESN;
I think maybe this is what you need:
UPDATE Routes b
SET URL = 'ok'
WHERE EXISTS
(SELECT * FROM STx a
WHERE a.GroupID = 39
AND a.Status = 'Tested'
AND a.RouteID = b.RouteID)
This will set the desired URL values to 'ok'.
You just prefix it with INSERT INTO table (columns). If you are trying to find b.URL values in your SELECT, then add a filter.
insert into desire_table (col1,col2,col3,...) SELECT
a.ESN,
b.URL,
a.Status,
a.GroupID,
a.RouteID
FROM STx a
LEFT JOIN Routes b
ON a.RouteID = b.RouteID
WHERE a.GroupID = 39
AND a.Status = "Tested"
order by a.ESN;
Remember that the output columns has to be matched in order to be able to sit in the new table, orders mather it means your first selected column goes to col1 and second one goes to col2 so you have to match the col1,col2,and so on with your select output

GROUP_CONCAT in IN Subquery

SELECT A.id, A.title,
FROM (`table`) as A
WHERE A.active = '1'
AND A.id IN (SELECT GROUP_CONCAT(B.id) from B where user = 3)
If i launch subquery SELECT GROUP_CONCAT(B.id) from B where user = 3 only, i obtain 1,2,3,4. But if i launch entire query i obtain only one row.
But if i try to substitute the subquery with its value (1,2,3,4)
SELECT A.id, A.title,
FROM (`table`) as A
WHERE A.active = '1'
AND A.id IN (1,2,3,4)
i obtain the 4 rows ... as i need.
Where is my error ?
MySQL is seeing the subquery return only a single field/row, and therefore treats it as something like:
... and A.id IN ('1,2,3,4')
which boils down to A.id = '1,2,3,4'.
For an 'in' query, there's no need for the group_concat stuff, simply do:
... and A.id IN (select B.id FROM b where user = 3)
SELECT name
FROM test
WHERE salry
IN (
SELECT GROUP_CONCAT( CONCAT('''', salry,'''' ) )
FROM test group by salry
)
this concat will append the resultset with single quotes still its not working like salry will be in resultset '1000','2000','3000','40000' ...
Use FIND_IN_SET()
SELECT A.id, A.title,
FROM (`table`) as A
WHERE A.active = '1'
AND FIND_IN_SET(A.id,(SELECT GROUP_CONCAT(B.id) from B where user = 3))