sqlte3 error no such column - sql

I'm trying to run this command from the shell:
sqlite3 salesShare1.db "
select
UsersSale.saleSpecificProduct,
UsersSale.fAtMall,
Users.name,
Users.gender,
Stores.storeName
FROM
UsersSale
INNER JOIN (Users INNER JOIN Stores ON Users.userID = UsersSale.userID) ON
Stores.storeID = UsersSale.saleStoreID
ORDER BY UsersSale.saleID";
Error: no such column: Users.name
I created all the tables and columns.
sqlite3 salesShare1.db "select * FROM Users ";
1|hezi|0|||
'hezi' is Users.name
What am I doing wrong ?

try please
select us.saleSpecificProduct,
us.fAtMall,u.name,
u.gender,st.storeName
from UsersSale us,Users u,Stores st
where u.userID=us.userID and st.storeID=us.saleStoreID
order by us.saleID
or
select us.saleSpecificProduct,
us.fAtMall,u.name,
u.gender,st.storeName
from UsersSale us
inner join Users u
on u.userID=us.userID
inner join Stores st
on st.storeID=us.saleStoreID
order by us.saleID
hope it helps

Display the schema for your table in the sqlite3 tool and confirm your column name is what you thought it was. Maybe you mis-typed the name.

Your inner join is a little ackward.
Shot in the dark, but my guess is you need to join in the Users table:
select
us.saleSpecificProduct,
us.fAtMall,
u.name,
u.gender,
s.storeName
FROM
UsersSale us
INNER JOIN Users u ON u.userID = us.userID
INNER JOIN Stores s ON s.storeID = us.saleStoreID
ORDER BY us.saleID

Related

How to join tables having many to many relationship

I am trying to JOIN four tables together, one table is a joining table as two of the tables have many-to-many relationship:
What would be the query to select DispalyName from User, Name & Description from Role and Permission & Description from Permission.
I know how to join two tables together, but my method of doing so does not work on this problem.
I have tried the following query but it doesn't seem to like it.
SELECT org.[User].[DisplayName], org.[Role].[Description], org.[Permission].[Description]
FROM org.[Role] rolee
JOIN org.[RolePermissions] rolePerms ON rolee.ID = rolePerms.RoleId
JOIN org.[Permission] perms ON rolePerms.PermissionId = perms.ID
WHERE [User].[email] LIKE '%myemail%'
Try this - It seems that You forget to join with the user table
SELECT org.[User].[DisplayName], org.[Role].[Description], org.[Permission].[Description]
FROM org.[User] user Join org.[Role] rolee on user.RoleID = rolee.ID
JOIN org.[RolePermissions] rolePerms ON rolee.ID = rolePerms.RoleId
JOIN org.[Permission] perms ON rolePerms.PermissionId = perms.ID
WHERE org.[User].[email] LIKE '%myemail%'
You need to join User
SELECT u.[DisplayName], r.[Description], p.[Description]
FROM org.[Role] r
JOIN org.[RolePermissions] rp ON r.ID = rp.RoleId
JOIN org.[Permission] p ON rp.PermissionId = p.ID
JOIN org.[User] u ON r.Id = u.RoleID
WHERE u.email LIKE '%myemail%'

SQL Where on different table

SELECT * FROM student_mentor sm INNER JOIN users u
ON sm.student_id = u.user_id
WHERE sm.teacher_id = $teacher_id
Teacher_id being the session id,
I want to see all the students that have the same mentor.
Right now if I run this I just see all of the students twice, maybe one of you knows why?
My db scheme
You are not specifying on which columns you want to do the join, so you're getting a cross reference where all records are joined to all records.
You should do something like (not sure about your column names):
SELECT * FROM student_mentor sm INNER JOIN users u
ON sm.student_id = u.user_id
WHERE sm.teacher_id = $teacher_id

Two inner joins with same tables

I have this problem I've been working on for a while, and I just haven't been able to figure out what's wrong with my query. Today, I finally got a bit closer, and now I think I'm pretty close and not too far from succeeding, but I need your help to spot the mistake in this query, because I think I've stared blind on it. This is the error I get:
"The objects "webpages_UsersInRoles" and "webpages_UsersInRoles" in the FROM clause have the same exposed names. Use correlation names to distinguish them."
SELECT * FROM Users
INNER JOIN webpages_UsersInRoles
ON webpages_UsersInRoles.UserId = Users.UserId
INNER JOIN webpages_UsersInRoles
ON webpages_UsersInRoles.RoleId = webpages_Roles.RoleId
WHERE Users.UserId = 1
Thank you in advance!
You have to use an alisas for the tables:
SELECT * FROM Users
INNER JOIN webpages_UsersInRoles w1 ON w1.webpages_UsersInRoles.UserId = Users.UserId
INNER JOIN webpages_UsersInRoles w2 ON w1.webpages_UsersInRoles.RoleId = w2.webpages_Roles.RoleId
WHERE Users.UserId = 1
I think below is the final query that you need. I have used alias as TAB1,TAB2 and TAB3 for clarity and to avoid confusion. Change it as required.
SELECT * FROM Users TAB1
INNER JOIN webpages_UsersInRoles TAB2
ON TAB2.UserId = TAB1.UserId
INNER JOIN webpages_Roles TAB3
ON TAB2.RoleId = TAB3.RoleId
WHERE TAB1.UserId = 1
Btw,JUST FOR INFO.... probably you get "The objects "webpages_UsersInRoles" and "webpages_UsersInRoles" in the FROM clause have the same exposed names. Use correlation names to distinguish them." error in the line mentioned below from your original query:
SELECT * FROM Users
INNER JOIN webpages_UsersInRoles
ON webpages_UsersInRoles.UserId = Users.UserId
INNER JOIN webpages_UsersInRoles
ON webpages_UsersInRoles.RoleId -----> Not able to identify which webpages_UsersInRoles is being called (First inner join or second inner join)
= webpages_Roles.RoleId
WHERE Users.UserId = 1
I see 3 tables ... Users, webpages_UsersInRoles and webpages_Roles
Only 2 are called.
webpages_Roles ??
Maybe with webpages_Roles in the second join :
SELECT * FROM Users
INNER JOIN webpages_UsersInRoles
ON webpages_UsersInRoles.UserId = Users.UserId
INNER JOIN webpages_Roles
ON webpages_UsersInRoles.RoleId = webpages_Roles.RoleId
WHERE Users.UserId = 1

SQL QUERY to get Count of particular column between two Range of Dates

Here I have a doubt regarding sql query.
In this scenario I have a table called tblcrime : where we will get the sum(crime) here I track MainID and sum(crime) query will be like this :
SELECT sum(o.crimeID) as crimeNumber,u.UserID
from tblcrime o
inner join tblSubContractor ts on
o.MainID=ts.SubContractorID
from here I will chk the tblUSER with these subcontractorID :
inner join tblUser u on
u.SubContractorID=ts.SubContractorID
and my doubt is that up to here I will get the total sum of crime and appropriate userid., for e.g.
UserID : 520 Totalcrime:6000
but there is another table called tblAudit where we will get logondate and userid, which is tracking here.. so I want to display crime based on userlogin(userid) ...since last login. So that when user login it shows in a jquery notification that "60 crimes has been done since last login".
I want help in query format.
I'm not sure, if I understand your question right, but may this be, what you are looking for?
SELECT sum(o.crimeID) as crimeNumber,u.UserID
from tblcrime o
inner join tblSubContractor ts on o.MainID=ts.SubContractorID
inner join tblUser u on u.SubContractorID=ts.SubContractorID
where
u.UserID = theOneYouAreLookingFor
AND crimedate >= lastLogOn
GROUP BY u.UserID
Firstly, I suspect that the call to the SUM function should really be to COUNT. The former adds the values of the specified column together, whereas the latter gives you a row count.
Secondly, does your tblcrime table store the date that crimes are added? I'll assume it does, let's call the column DateAdded. The following query should work:
SELECT COUNT(o.crimeID) AS crimeNumber,
u.UserID
FROM tblcrime o
INNER JOIN tblSubContractor ts on o.MainID = ts.SubContractorID
INNER JOIN tblUser u on u.SubContractorID = ts.SubContractorID
INNER JOIN tblAudit a on a.userid = u.UserID
WHERE a.logondate < o.DateAdded
GROUP BY u.UserID
You could find the max auditdate for that user:
SELECT sum(o.crimeID) as crimeNumber,u.UserID
from tblcrime o
inner join tblSubContractor ts on
o.MainID=ts.SubContractorID
inner join tblUser u on
u.SubContractorID=ts.SubContractorID
where o.crimeDate >= (select max(auditdate) from tblAudit where UserID = #UserID)

SQL Query - Multiple Joins on Same field

I need assistance building this query where i need to select different values from same table but different Unique Keys.
To elaborate more ill provide the below example:
I have 2 tables:
Issues (IssueID, AuthorID_FK, AssigedID_FK, ... )
Users (UserID, User_Label, ... )
Both AuthorID_FK & AssigedID_FK are linked to table Users and i need to get in the same query result the User_Label for both.
Can you please assist?
Thanks,
SELECT a.IssueID, b.User_Label, c.User_Label FROM Issues a
INNER JOIN USERS b on a.AuthorID_FK = b.UserID
INNER JOIN USERS c on a.AssignedID_FK = c.UserID
something like that :) This should work in MS SQL Server
Well, this one should work too :)
SELECT IssueID, U.User_Label FROM Issues I
INNER JOIN Users U ON U.UserID = I.AuthorID_FK
UNION
SELECT IssueID, U.User_Label FROM Issues I
INNER JOIN Users U ON U.UserID = I.AssigedID_FK
Wont
SELECT a.IssueID, b.UserID
FROM Issues a
JOIN Users b ON (a.AuthorID_FK=b.UserID OR a.AssignedID_FK = b.UserID)
work?
You may want to try something like this
SELECT
issues.IssueID,
Authour.User_Label AS Author_Label,
Assigned.User_Label AS Assigned_user_Label
FROM
issues
INNER JOIN users AS Authour ON Authour.UserID = issues.AuthorID_FK
INNER JOIN users AS Assigned ON Assigned.UserID = issues.AssignedID_FK