I have the following query. If I run it I get this error message.
Query-
SELECT account_name,ABC,date FROM entries
LEFT JOIN accounts ON accounts.id = entries.accounts_id
LEFT JOIN voucher ON voucher.id = entries.trans_id
WHERE trans_id IN ( SELECT trans_id, amount AS ABC FROM entries
WHERE accounts_id='$accounts_id' AND side='C')
AND accounts_id!='$accounts_id' AND side='D'
AND voucher.date between '$dateragne1' AND '$dateragne2'
I think the problem is with the value ABC. It is unable to fetch the value from the second query.
Could you please tell me how to fix this query?
Thanks in Advance :)
Try this:
SELECT account_name, _inner.ABC, date
FROM
(
SELECT amount AS ABC FROM entries
WHERE accounts_id='$accounts_id' AND side='C'
) AS _inner, entries
LEFT JOIN accounts ON accounts.id = entries.accounts_id
LEFT JOIN voucher ON voucher.id = entries.trans_id
WHERE trans_id IN
(
SELECT trans_id FROM entries WHERE accounts_id='$accounts_id' AND side='C'
)
AND accounts_id!='$accounts_id' AND side='D'
AND voucher.date between '$dateragne1' AND '$dateragne2'`
Notes:
Using subquery like this doesn't allow you to request a fields from it.
Also, IN statement use data from only only column, not two.
Related
I have a MySQL query that outputs to a php table but I'm having issues in joining two tables that both use a COUNT:
$query = "SELECT mqe.registration,
COUNT(*) AS numberofenqs,
COUNT(DISTINCT ucv.ip) AS unique_views,
SUM(ucv.views) AS total_views
FROM main_quick_enquiries AS mqe
LEFT OUTER JOIN used_car_views AS ucv
ON ucv.numberplate = mqe.registration
WHERE mqe.registration IS NOT NULL
GROUP BY mqe.registration ORDER BY numberofenqs DESC";
The query runs, but the number within the numberofenqs column is always wrong as i know from performing that query on its own that it comes in with the correct result:
SELECT registration, COUNT(*) AS numberofenqs FROM main_quick_enquiries GROUP BY registration ORDER BY numberofenqs DESC
Why is the COUNT(*) not working correctly in top query code and where is it getting the figures from?
it could be because of LEFT OUTER JOIN ...
Try to run this:
SELECT registration
, count(*)
FROM main_quick_enquiries
GROUP BY registration
and compare it with this result
SELECT mqe.registration
, count(*)
FROM main_quick_enquiries mqe
LEFT OUTER JOIN used_car_views ucv
ON ucv.numberplate = mqe.registration
GROUP BY mqe.registration
There could be a problem :) in duplicity rows... try to find one specific registration number, and compare the details of both query
SELECT *
FROM main_quick_enquiries
WHERE registration = XXXX
+
SELECT *
FROM main_quick_enquiries mqe
LEFT OUTER JOIN used_car_views ucv
ON ucv.numberplate = mqe.registration
WHERE registration = XXXX
you should see the diffs
Thanks All, but I think I've nailed it with COUNT(DISTINCT mqe.id) instead of COUNT(*).
I am wanting to display results where the date stored in the table is not between the dates specified in the query.
Here is the current SQL query
SELECT accounts_cstm.statusdescription_c,
users.user_name,
accounts.name,
accounts_cstm.account_number_c,
DATE_FORMAT(MAX(calls.date_modified),'%Y/%m/%d')
FROM accounts
LEFT OUTER JOIN calls ON accounts.id = calls.parent_id
LEFT OUTER JOIN users ON accounts.assigned_user_id = users.id
LEFT OUTER JOIN accounts_cstm ON accounts.id = accounts_cstm.id_c
AND accounts.deleted <> 1
WHERE
(SELECT DATE_FORMAT(MAX(calls.date_modified),'%Y/%m/%d')
FROM calls) NOT BETWEEN '2014/06/25' AND '2014/07/02'
AND users.user_name = 'CBennet'
AND accounts_cstm.chkcustomer_c = '1'
GROUP BY accounts.name
I get a full list of results but I get results that shouldn't appear ie results with calls.date_modified that is in between the dates specified.
See below for an example of a wrong result, you can see that the date to the right is in between the dates 2014/06/25 and 2014/07/02 therefore this shouldn't appear.
Can someone let me know what i'm doing wrong here?
Within the table calls, date_modified is stored in the following format 2014-06-10 10:55:47
try this
SELECT *
FROM `test`
WHERE (date NOT BETWEEN '2012-01-30 14:15:55' AND '2014-09-29 10:15:55')
I created test table with some test values at sqlfiddle and got desired output http://sqlfiddle.com/#!2/5ffb7/4
I have the following 2 queries.
Query 1 :
select distinct(thread_id) from records where client_name='MyClient'
Query 2 :
select max(thread_no) from records
where thread_id='loop_result_from_above_query' AND action='Reviewed'
Is it possible to combine them into a single query ?
The second query is run on every result of the first query.
Thank you.
See attached image of a small snippet of mysql records.
I need a single mysql query to output only records which have action="MyAction" as the latest records for a given set of thread_ids. In the sample data set : record with Sr: 7201
I hope this helps in helping me :)
SELECT client_name, thread_id, MAX(thread_no) max_thread
FROM records
WHERE action='Reviewed' AND client_name='MyClient'
GROUP BY client_name, thread_id
UPDATE 1
SELECT a.*
FROM records a
INNER JOIN
(
SELECT thread_id, max(sr) max_sr
FROM records
GROUP BY thread_id
) b ON a.thread_id = b.thread_id AND
a.sr = b.max_sr
WHERE a.action = 'MyAction'
You can use SELF JOIN, but it is not advisable and will impact your query performance. Please check below query for your reference
SELECT DISTINCT r1.thread_id, MAX(r2.thread_no) from records r1 LEFT JOIN records r2 ON r2.thread_id=r1.thread_id WHERE r1.client_name='MyClient' AND r2.action='Reviewed'
SELECT a.maxthreadid,
b.maxthreadno
FROM (SELECT DISTINCT( thread_id ) AS MaxThreadId
FROM records
WHERE client_name = 'MyClient') a
CROSS JOIN (SELECT Max(thread_no) AS MaxThreadNo
FROM records
WHERE thread_id = 'loop_result_from_above_query'
AND action = 'Reviewed') b
Try this.
SELECT *
FROM (SELECT Row_number()
OVER (
partition BY thread_id
ORDER BY thread_no) no,
Max(thread_no)
OVER(
partition BY thread_id ) Maxthread_no,
thread_id,
action,
client_name
FROM records
Where client_name = 'MyClient') AS T1
WHERE no = 1
AND action = 'Reviewed'
I need to exclude the results from a complex query from another query. I don't know how to make a LEFT JOIN work with the results of another JOIN query.
I want to return fields from lt.contacts after subtracting (excluding) the result of this:
(SELECT `contacts`.`idContacts` AS id, `contacts`.`First_Name`, `contacts`.`Last_Name`
FROM `lt`.`contacts`
JOIN `lt`.`groups`
JOIN `lt`.`groups_has_contacts`
ON Contacts_idContacts=idContacts
WHERE idGroup
IN (35)
AND Groups_idGroup
IN (35))
From the results of this:
SELECT * FROM `lt`.`groups_has_contacts` Where `Groups_idGroup` = 37)
I'm pulling my hair out -- any help before I am bald would be appreciated!
Try this:-
SELECT contacts.idContacts, contacts.First_Name, contacts.Last_Name
FROM contacts, groups
where contacts.idContacts=groups.idgroup
OR
SELECT contacts.idContacts, contacts.First_Name, contacts.Last_Name
FROM contacts INNER JOIN groups ON contacts.idContacts=groups.idgroup
I have a member Table with member_Id
a mmship table with columns mmshipstart date and member_Id and mshipstatus_Id
another table mshipstatustype with columns mshipstatus_Id and mshipstatus_name
I have got mshipstatus_name row value is prospective......
how do i get the number of members(count) per month those are having mshipstatusname is prospective
can i get the count starting from mmshipstart date .
would any one help me out...
I am new to joins would any one pls help....
The following should do the trick:
SELECT COUNT(*)
FROM member inner join mmship
ON member.member_Id = mmship.member_Id
INNER JOIN mshipstatus
ON mshipstatus.mshipstatus_Id = mmship.mshipstatus_Id
WHERE mshipstatus.mshipstatusname = 'prospective'
AND MONTH(mmship.mmshipstart_date) = MONTH(GETDATE())
(you can change the getdate() with another date).
Does
select count(*) as prospective from mshipstatustype t1 join mmship t2 where
t1.mshipstatus_id=t2.mshipstatus_id and t2.mshipstatus_name="prospective" group by
year(mmshipstart_date), month(mmshipstart_date)
Do what you want?