简体   繁体   中英

How to get count value from two table using alias name

I have two tables, one export_log and another one is client_log table first I tried to get count value from single table( export_log ) which worked correctly. And then I tried to get count value from two tables( export_log and client log ), now I have an error like :

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* ) Total_login, cl.count(*) Total_access DATE_FORMAT( max( el.lastmodified ) ' at line 1

I need to get count value from two tables with single query, I have tried so many times I didn't get the results. And also how to use alias name inside max() function?

pls correct this query..

 SELECT el.count( * ) Total_login, cl.count(*) Total_access DATE_FORMAT( max( `el.lastmodified` ) , '%d %M %y ,%H:%i' ) last_logged
    FROM export_log el, client_log cl
    WHERE (el.unique_id = 'n110r24dan756j5vnv5v0016r31ad6jg' and el.unique_id != 'nn10r24daj756j5hnv5v0016r31ad6ja') AND (cl.unique_id = 'n110r24dan756j5vnv5v0016r31ad6jg' and cl.unique_id != 'n110r24dan756j5vnv5v0016r31ad6jg')
    AND el.user_name
    IN (
    'bala','sathish'
    ) AND cl.user_name IN('bala','sathish')
    AND `el.lastmodified`>=CAST(DATE_FORMAT( NOW( ) , '%Y-%m-01' ) AS DATE)and CAST(DATE_FORMAT(NOW(),'%Y-%m-01') AS DATE)
    GROUP BY el.unique_id,cl.unique_id
    ORDER BY Total_login DESC

Because your syntax is wrong at select clause so you can try:

SELECT count(el.*) Total_login, count(cl.*) Total_access, DATE_FORMAT( max(`el.lastmodified`) , '%d %M %y ,%H:%i') last_logged
    FROM export_log el, client_log cl
    WHERE (el.unique_id = 'n110r24dan756j5vnv5v0016r31ad6jg' and el.unique_id != 'nn10r24daj756j5hnv5v0016r31ad6ja') AND (cl.unique_id = 'n110r24dan756j5vnv5v0016r31ad6jg' and cl.unique_id != 'n110r24dan756j5vnv5v0016r31ad6jg')
    AND el.user_name
    IN (
    'bala','sathish'
    ) AND cl.user_name IN('bala','sathish')
    AND `el.lastmodified`>=CAST(DATE_FORMAT( NOW( ) , '%Y-%m-01' ) AS DATE)and CAST(DATE_FORMAT(NOW(),'%Y-%m-01') AS DATE)
    GROUP BY el.unique_id,cl.unique_id
    ORDER BY Total_login DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM