I have 2 tables in mysql - User (user_id, first_name ....) and login_history(user_id, login_time)
Every time an user loges in, system records the time in login_history.
I want to run a query to fetch all the fields from the users table and the latest login time from login_history . Can anyone help please?
You have to use a join then :
SELECT *, login_history.login_time
FROM User
INNER JOIN login_history
ON User.user_id=login_history.user_id;
And this query gonna give you, all the columns of User and the login_time.
SELECT t1.col1
,t1.col2
,[...repeat for all columns in User table]
,max(t2.login_time)
FROM user t1
INNER JOIN login_history t2 ON t1.user_id = t2.user_id
GROUP BY t1.col1
,t1.col2
,[..repeat for all columns in User table]
This should work, assuming login_time is stored in a sane data type and/or format.
Following are 2 queries that can help you out to select latest login time with user details
SELECT * FROM User C,login_history O where C.user_id=O.user_id order by O.login_time desc limit 1
or
SELECT * FROM User C,login_history O where C.user_id=O.user_id and ROWNUM <=1 order by O.login_time desc
Related
have the following DB structure in MySQL:
Table1: ORDER
With Attributes
ID (PRIMARY KEY)
DELIVERY_DATE
CUSTOMER_ID
RESTAURANT_ID
ORDER_VALUE
Table2 : CUSTOMER
With Attributes
ID (PRIMARY KEY)
NAME
LOGIN
Table 3: Restaurant
ID (PRIMARY KEY)
NAME
CITY
I want to generate a db query where I select the customer_ID, login, Delivery_date, Sequence number, And the number of days from the last order to the second to last order.
I am having trouble using Inner join and generating the last two columns of my query:
Sequence: Each user may have ordered any number of dishes, I want to show a number three if it is the third time he orders, I should be able to get it from his customer_ID and the Order_ID
DateDiff from last order: I am having trouble getting the last and the second to last order and doing a dateDIFF and populating the column
I have so far constructed the following query:
SELECT customer_id,
login,
delivery_date,
sequence,
Datediff(second_to_last_order, last_order)
FROM order AS t1
INNER JOIN customer AS t2
ON t1.customer_id = t2.id
INNER JOIN restaurant AS t3
ON t1.restaurant_id = t3.id;
But it is obviously incomplete but i am stuck, any suggestions?
If this is MySQL Server query as you mentioned then I'd suggest you give the following a go;
SELECT CUSTOMER_ID, LOGIN, DELIVERY_DATE, SEQUENCE, DATEDIFF((SELECT MAX(z.ORDER_DATE) FROM ORDER AS z WHERE z.CUSTOMER_ID = t1.CUSTOMER_ID), (SELECT MAX(x.ORDER_DATE) FROM ORDER AS x WHERE x.CUSTOMER_ID = t1.CUSTOMER_ID AND x.ID != (SELECT MAX(x.ID) FROM ORDER AS x2 WHERE x2.CUSTOMER_ID = t1.CUSTOMER_ID)))
FROM ORDER AS a
INNER JOIN CUSTOMER AS t2 ON t1.CUSTOMER_ID = t2.ID
INNER JOIN RESTAURANT AS t3 ON t1.RESTAURANT_ID = t3.ID
ORDER BY t1;
I would advice that it's good practise to not call a table "ORDER" as this is a keyword used by MySQL, you could instead simple call the table "ORDERS" to avoid the issue.
This should do what you are asking for, if you have any issue please post any error text so we can better understand what you are facing.
I currently have the following (simplified) sql which selects user data from a main table table1. This query only select users who have not logged in since a given date. I use a sub query as the login table is separate from the main table. The two tables relate via a user ID column. This works perfectly.
// sql to get all records that haven't logged in since date above
$sql = "SELECT t1.*
FROM table1 t1
WHERE '2016-08-01' > (SELECT MAX(t2.DateCol)
FROM table2 t2
WHERE t2.userId= t1.userId)";
My question is, is there a way of returning the value of MAX(t2.DateCol)?
I tired this but is didn't reconise the column
$sql = "SELECT t1.*, MAX(t2.DateCol) ....
FYI: This sql is parsed into a custom PDO function so no need to warn about insecurities of mysql functions.
A quick solution (not knowing your skillset or data setup) - Move the sub-query in scope.
$sql = "SELECT t1.*,
(SELECT MAX(t2.DateCol)
FROM table2 t2
WHERE t2.userId= t1.userId) AS LastLogin
FROM table1 t1
HAVING '2016-08-01' > LastLogin ";
You will need to use HAVING instead of WHERE because you are comparing using an alias. If your query cannot use HAVING due to other factors then you'll need to repeat the subquery (not ideal).
If you want to show the aggregation result, you should select from the aggregation. In order to do so group your aggregation by userid. You can use HAVING to only select desired ones.
select t1.*, t2.max_datecol
from table1 t1
join
(
select userid, max(datecol) as max_datecol
from table2
group by userid
having max(datecol) < date '2016-08-01'
) t2 on t2.userid = t1.userid;
I have one database with two tables:
table1: "backup1"
table2: "backup2"
both tables has a structure like
id, backupid, userid, backup, info
I would like to do a simple MYSQL request to get a group of entries where
it shows me what userid is found in table1 and ALSO in table2 to see what users did backups in both tables
I know I could open table1 look for a userid and do a another MYSQL to see if its also
in table2, but I hope there is a simpler way to do that with a single request.
SELECT userid
FROM backup1
INNER JOIN backup2
USING(userid)
The inner join on the userid field will cause the query to only return rows that are found in both tables.
This isn't through '2 databases', this is through '2 tables'. Just use a join.
SELECT a.userid
from backup1 a
inner join backup2 b
on a.userid=b.userid
This will give you all userids in backup1 that also exist in backup2
if you only need to know if a user made a backup in both tables, you could simply use simple select statements. A simple example, using a temporary variable #uId to hold the Id of the user you want to check, and two temporary variables to hold the (boolean) answer for each table
set #uId = 1; -- The user Id
select
#b_t1 := (select count(*) from table1 where userId=#uId) > 0 as hasBackupOnTable1,
#b_t2 := (select count(*) from table2 where userId=#uId) > 0 as hasBackupOnTable2,
#b_t1 and #b_t2 as hasBackupOnBothTables;
I need to select data from 3 mysql database tables, tally up various results from 1 table (points), order by highest points to lowest and show only 100 results.
I have this query which I believe may be on the cusp of success but not quite.
The 3 tables are users, dealerships and sales_list.
Your assistance with achieving the above and correcting the query is appreciated.
$query = " SELECT t1.*, t2.*, t3.sales_points
FROM users t1
JOIN dealerships t2
ON t1.dealership_id = t2.dealership_id
INNER JOIN sales_list t3
ON t1.users_sales_guild_id = t3.users_sales_guild_id
ORDER BY t3.sales_points
LIMIT 100";
I have two tables, one holds user info (id, name, etc) and another table that holds user tickets and ticket status (ticket_id, user_id, ticket_status, etc).
I want to produce a list of ALL the users for example: ( SELECT * FROM user_table )
And for each user I need a count of their tickets for example:
(SELECT t1.user_id, COUNT(*) FROM user_tickets t1 WHERE t1.ticket_status = 15 GROUP BY t1.ticket_status, t1.user_id )
I can do this query to achieve what I’m looking for but it takes 5sec. to run the query on 50000 tickets, while each query running separately only takes fraction of a second.
SELECT t1.user_id, COUNT(*)
FROM user_tickets t1
LEFT JOIN user_table t2 ON t1.user_id = t2.id
WHERE t2.group_id = 20 AND t1.status_id = 15
GROUP BY t1.status_id, user_id
Any idea how to write the query to get same performance as each separately?
An indexing where clause fixed the problem.