how to select the last row insert by a specific iduser - mysql

Hello I want to get a specific userid who enter specific data. When X user enter data and save h will be the last row. But How I take the last row of exemple my user id = 16. Mnay user can insert data after him but I want only the last row of my user id 16.
SELECT *
from
projetstaches
inner join
timesheets
on (timesheets.timId = projetstaches.prtTimeSheetId)
inner join
users
on (users.usrId = timesheets.timUserId)
WHERE
users.usrId = 16 AND `prtTimeSheetId` = (
SELECT MAX( `prtTimeSheetId` )
FROM projetstaches )
When I do something like this it's return blank cause the last user who entered something is 7.
So how do I retrieve my last user = 16?
Here what look my table ( this is a old ver but good to know how it's look like the data now is more completed but the name of table its the same)
http://pastebin.com/6LBwGtc3

The subquery needs to use a join to select the highest prtTimeSheetId for this user, not the entire table.
SELECT *
from
projetstaches
inner join
timesheets
on (timesheets.timId = projetstaches.prtTimeSheetId)
inner join
users
on (users.usrId = timesheets.timUserId)
WHERE
users.usrId = 16
AND prtTimeSheetId = (
SELECT MAX(preTimeSheetId)
from
projetstaches
inner join
timesheets
on (timesheets.timId = projetstaches.prtTimeSheetId)
inner join
users
on (users.usrId = timesheets.timUserId)
WHERE users.usrId = 16)

Related

How to check user's permission based on 2 fields - mysql

Have 2 tables
User table with
user chat_id, kunnr (cust_code) & vkorg
Delivery table with
PO # and other delivery info, cust_code & sales_org
I want to verify if the user cust_code and sales_org match with shipmentdel's table
One user can belong to multiple sales org and a PO can have many delivery orders
Below is my working SQL statement, can this be simplified ?
SELECT * FROM ( SELECT a.* FROM ztelegram.ShipmentDel a
WHERE a.Cust_PO = 'WPO-01502') as t1
LEFT JOIN
( SELECT b.* FROM ztelegram.noti_setting b WHERE b.chat_id = '12345667' ) as t2
ON t1.sales_org = t2.vkorg AND t1.Sold_to_party_name = t2.kunnr
Without the need of t1 & t2, isnt the query working? Please check.
SELECT
*
FROM
ztelegram.ShipmentDel a
LEFT JOIN ztelegram.noti_setting b ON a.sales_org = b.vkorg AND a.Sold_to_party_name = b.kunnr
WHERE
a.Cust_PO = 'WPO-01502'
and b.chat_id = '12345667'

SQL: count rows from 3rd table

I have 3 tables, the first table is the account_has_account1 where i store the relation between accounts and it's columns are account_id, account_id1, status where account_id is the account doing following to account_id1 and status is an enum type with values active, inactive where active indicates if the account is actually following, if it's inactive, then account stopped following.
the second table is named account_has_photos which i store the photos one account has stored in the database, so it's columns are account_id, photos_id, so i need this table to get all photos from one account which another account is following.
But all these have messages posted on them, and here is where the 3rd table comes which is named photos_has_message_photos, from this table i only need a count of all posted messages in one photo, the columns are photos_id, message_photos_id
for now my query is this:
SELECT account_has_photos.photos_id as id, "photos" as type, account_has_photos.update_at, account_has_photos.account_id
FROM account_has_account1
JOIN account_has_photos
ON (account_has_photos.account_id = account_has_account1.account_id1 AND account_has_photos.type_id = 17)
WHERE account_has_account1.account_id = 7 AND account_has_account1.`status` = "Active"
it shows all photos from accounts on which account id 7 is following, but on my attempts on getting the total messages have failed, i thought on doing an INNER JOIN like this:
INNER JOIN (
SELECT photos_has_message_photos.photos_id, count(photos_has_message_photos.photos_id) as total
FROM photos_has_message_photos
) posts
ON(posts.photos_id = account_has_photos.photos_id)
and then i select from main posts.total, but it does not show any row, not even the photos, the result is empty at this point and i have no idea why and what to do.
the complete query is like this:
SELECT account_has_photos.photos_id as id, "photos" as type, account_has_photos.update_at, account_has_photos.account_id, posts.total
FROM account_has_account1
JOIN account_has_photos
ON (account_has_photos.account_id = account_has_account1.account_id1 AND account_has_photos.type_id = 17)
INNER JOIN (
SELECT photos_has_message_photos.photos_id, count(photos_has_message_photos.photos_id) as total
FROM photos_has_message_photos
) posts
ON(posts.photos_id = account_has_photos.photos_id)
WHERE account_has_account1.account_id = 7 AND account_has_account1.`status` = "Active"
again, i only need a total of rows which are messages from each photos found
Try this query updated inner select
SELECT ahp.photos_id as id, "photos" as type, ahp.update_at, ahp.account_id,posts.total
FROM account_has_account1
JOIN account_has_photos
ON (ahp.account_id = account_has_account1.account_id1 AND ahp.type_id = 17) INNER JOIN (
SELECT phmp.photos_id, count(*) as total FROM photos_has_message_photos GROUP BY phmp.photos_id
) posts
ON(posts.photos_id = ahp.photos_id) WHERE account_has_account1.account_id = 7 AND account_has_account1.`status` = "Active"

Selecting data for 1 specific user from multiple tables

So my database is composed of 5 tables with different columns for each one. The only column that keeps them all identified is the id. I'm trying to get the data for a specific user, but I only seem to get all users of the database instead.
This is what I have tried:
SELECT
ControlAccess.UserName,
ControlAccess.Pass,
Users.First_Name,
Users.Last_Name,
UserInfo.Age,
UserInfo.Country,
UserInfo.Address,
UserInfo.ZipCode,
Sessions.Matrix1,
Sessions.Matrix2,
Sessions.Result,
Operations.Operation,
FilePath.LocationFiles
FROM
MatrixUsers.UserInfo
INNER JOIN
MatrixUsers.Users
ON
UserInfo.idUserInfo = Users.idUsers = 1
INNER JOIN
MatrixUsers.ControlAccess
ON
ControlAccess.idControlAccess = UserInfo.idUserInfo = 1
INNER JOIN
MatrixUsers.Sessions
ON
Sessions.idSessions = ControlAccess.idControlAccess = 1
INNER JOIN
MatrixUsers.FilePath
ON
FilePath.idFilePath = Sessions.idSessions = 1
INNER JOIN
MatrixUsers.Operations
ON
Operations.idOperations = FilePath.idFilePath = 1;
I tried putting 1 at the end of each id to see if they matched, but I still get all the users.
I'm new to SQL and I'm only familiar with matching rows, but not choosing specific one.
Here are the columns of each table:
ControlAccess: {idControlAccess, UserName, Pass}
Sessions: {idSessions, Matrix1, Matrix2, Result}
FilePath: {idFilePath, LocationFiles}
Operations: {idOperation, Operation}
UserInfo: {idUserInfo, Age, Country, Address, ZipCode, Phone}
Use WHERE when you want specific user. for example, select user_id from table where user_id=the_specific_user_id . Follow this basic to built you complicate statement.
Just user where after all the joins
WHERE ANY_COLUMN_REFER_TO_USER_ID = YOUR_NEEDED_ID
so your full query would be like :
SELECT
ControlAccess.UserName,
ControlAccess.Pass,
Users.First_Name,
Users.Last_Name,
UserInfo.Age,
UserInfo.Country,
UserInfo.Address,
UserInfo.ZipCode,
Sessions.Matrix1,
Sessions.Matrix2,
Sessions.Result,
Operations.Operation,
FilePath.LocationFiles
FROM MatrixUsers.UserInfo
INNER JOIN MatrixUsers.Users
ON UserInfo.idUserInfo = Users.idUsers
INNER JOIN MatrixUsers.ControlAccess
ON ControlAccess.idControlAccess = UserInfo.idUserInfo
INNER JOIN MatrixUsers.Sessions
ON Sessions.idSessions = ControlAccess.idControlAccess
INNER JOIN MatrixUsers.FilePath
ON FilePath.idFilePath = Sessions.idSessions
INNER JOIN MatrixUsers.Operations
ON Operations.idOperations = FilePath.idFilePath
WHERE UserInfo.idUserInfo = 1

Showing List Where ID is not Present

I have two table,
first is
master_location:
this table is consist of column (id, location)
row 1, id = 1 , location = X
row 2, id = 2 , location = Y
row 3, id = 3 , location = Z
second is
user_access
column (email, id)
row 1 => me#localhost.host , 1
i want to show all other location from master location where email "me#localhost" is NOT present.
I've set up sql:
SELECT ml.idms_location, ml.location
FROM ms_location ml JOIN user_access tu
WHERE ml.id= tu.id
AND !(tu.email = 'me#localhost.host');
but it only show from table user_access where the email is NOT me#localhost.host
is there any sql syntax like SELECT ALL EXCEPT?
thanks
UPDATE
Table 1: master_location
Tabel 2: tm_access
i've write the syntax result as suggested:
SELECT DISTINCT ml.idms_location,ml.location
FROM ms_location ml
LEFT OUTER JOIN tm_userlocation tu
ON ml.idms_location = tu.idms_location
WHERE COALESCE(tu.email,'john#mscid.com') = 'john#mscid.com';
the result
USING THIS SQL SYNTAX:
SELECT ms.idms_location, ms.location
FROM ms_location AS ms LEFT JOIN tm_userlocation AS tu
ON ms.idms_location = tu.idms_location
WHERE tu.email !='john#mscid.com' OR tu.email IS NULL;
the result where i execute is only the first ID is missing, the no. 3 and no. 5 should removed too
screenshot:
Following statement will return all rows from ms_location that have no corresponding row in user_access.
SELECT ml.idms_location,ml.location
FROM master_location ml
WHERE NOT EXISTS (
SELECT *
FROM user_access tu
WHERE tu.idms_location = ml.idms_location
AND tu.email = 'john#mscid.com'
)
SQL Fiddle Demo
You've left a bit to be guessed at (I'm assuming your cardinality is 1..*), but I think what you're asking for is a result set showing the ms_location records that don't have an associated user_access record with the email address 'me#localhost.host'. That is, from your given sample data, only location X has an associated user_access record with that email address (it's also the only record). Locations Y and Z do not have associated user_access records with the specified address. These are the ones you want.
If I've interpreted you correctly, try this:
SELECT ml.idms_location, ml.location
FROM ms_location ml
WHERE ml.id NOT IN (
SELECT id FROM user_access WHERE email = 'me#localhost.host')
If I haven't, don't.
As per your data Your Query should be
select m.`id`, m.`location` from `master_location` as m
WHERE
m.`id` NOT IN ( select `id` from `user_access` WHERE `email` = 'john#mscid.com' ) ;
Check the SQL Fiddle Link
http://sqlfiddle.com/#!2/28b50/3

Trying to building optimised Query for Group and Subgroup for a user

i am trying to write the Query for three things .My table structure is like that
You can see Schema at http://sqlfiddle.com/#!2/56c2d/1
I am trying to write the query in MYSQL
user:- table
user_id
user_fname
This is User tabke which will save User Information
group:- "group" and "subgroup" is maintain in same table using column "group_parent_group_id"
group_id
group_title
group_parent_group_id(INT)
This is group table which will save Group and Subgroups
user_group: table
user_group_id
user_group_user_id
user_group_group_id
This ill store both User and Group relation using their Id
I am trying to write the Query for three things. Fetching Users Groups, Subgroups
1) Query to fetch list of All Groups for User Register. Query is gelow and is giving error
Query:
select user.id, user.user_fname, group.group_id, group.group_title
from `user`
inner join user_group on user_group.user_group_user_id = user.user_id
inner join group on group.group_id = user_group.user_group_group_id
where user_group.user_group_user_id = 1 and user_group.group_parent_group_id = 0
2) I am Looking the query to fetch all subgroups(For Whom user is already Register) for Group Id 1,2 or 1
3) I am Looking the query to fetch all subgroups(For Whom user is Not Register yet) for Group Id 1,2 or 1. Ideal is for giving him randomly suggest a subgroup to add
Please Help. I am a newbie in DB :(
Your query is probably failing as you have a table called group, which is a reserved word. You can use back tics to delimit the name to get away with this (as follows) but it would be a better idea to change the table name.
SELECT user.id, user.user_fname, `group`.group_id, `group`.group_title
FROM `user`
INNER JOIN user_group ON user_group.user_group_user_id = user.user_id
INNER JOIN `group` ON `group`.group_id = user_group.user_group_group_id
WHERE user_group.user_group_user_id = 1
AND user_group.group_parent_group_id = 0
EDIT updated for queries I think the OP requires.
First query will get a list of all the groups (ones that have no parent group id) that a user (in this case id 28) is a member of
SELECT y2m_user.user_id, y2m_user.user_first_name, y2m_group.group_id, y2m_group.group_title
FROM y2m_user
INNER JOIN y2m_user_group ON y2m_user_group.user_group_user_id = y2m_user.user_id
INNER JOIN y2m_group ON y2m_group.group_id = y2m_user_group.user_group_group_id
WHERE y2m_user.user_id = 28
AND y2m_group.group_parent_group_id = 0
This query will get a list of all the sub groups (ones where the parent group id is greater than 0) that a user (in this case id 28) is a member of
SELECT y2m_user.user_id, y2m_user.user_first_name, y2m_group.group_id, y2m_group.group_title
FROM y2m_user
INNER JOIN y2m_user_group ON y2m_user_group.user_group_user_id = y2m_user.user_id
INNER JOIN y2m_group ON y2m_group.group_id = y2m_user_group.user_group_group_id
WHERE y2m_user.user_id = 28
AND y2m_group.group_parent_group_id > 0
This query will get a list of all the sub groups (ones where the parent group id is greater than 0) that a user (in this case id 28) is NOT a member of
SELECT y2m_user.user_id, y2m_user.user_first_name, y2m_group.group_id, y2m_group.group_title
FROM y2m_user
CROSS JOIN y2m_group
LEFT OUTER JOIN y2m_user_group ON y2m_user_group.user_group_user_id = y2m_user.user_id AND y2m_group.group_id = y2m_user_group.user_group_group_id
WHERE y2m_user.user_id = 28
AND y2m_group.group_parent_group_id > 0
AND y2m_user_group.user_group_id IS NULL
Please excuse any typos as not tested (with your test data there are no sub groups).