visual studio and sql how to select multiple tables - mysql

First I made a login screen on visual basic with sql, but I need to get those passwords and usernames form two tables. So I have done as
Query = "select * from jofy.user jofy.artist where username='" & TextBox1.Text & "' and password='" & TextBox2.Text & "'"`
I need to get those information from both user and artist, but it always shows an error
i've tried using jofy.user, jofy.artist
jofy.user and jofy.artist
jofy.user or jofy.artist
if anyone knows how to fix this, I would appreciate help with it.

Use SQL JOIN
SQL JOIN is a method to retrieve data from two or more database tables.
example T-SQL:
select
user.*
,artist.*
from jofy.user as user
join jofy.artist as artist
on artist.userID = user.id
where
user.username = 'user.name'
and user.password = 'pass'
...

Related

Why is my query not being updateable stopping me updating a table with it?

I've trying to update a table using the following UPDATE query:
UPDATE tblPlayers_atp INNER JOIN qryMostRecentScoresOv _
& ON tblPlayers_atp.ID = qryMostRecentScoresOv.ID _
& SET tblPlayers_atp.ELO_Ov = [qryMostRecentScoresOv].[ID_nEloOv];
When I hit run I get "Operation must use an updateable query". The source query qryMostRecentScoresOv includes GROUP BY which I gather is the issue.
Now I've read quite a bit around this but what I don't understand is why this causes an issue? The action I'm performing doesn't update the query in any way - it just wants to use the data within it. I realise this might be a basic question but I've been reading for a while now and couldn't find the answer.
If it helps here is the SQL for the source query:
SELECT tt.ID, tt.ID_nEloOv
FROM qryUniScoresEloInc AS tt INNER JOIN (SELECT ID, MAX(PK_G) _
& AS [Max] FROM qryUniScoresEloInc WHERE ID_nEloOv is not null _
& GROUP BY ID) AS groupedtt ON (tt.ID = groupedtt.ID) _
& AND (tt.PK_G = groupedtt.Max)
ORDER BY tt.ID;
I'm now setting about creating a temporary table from the source query and update from that... https://www.fmsinc.com/MicrosoftAccess/query/non-updateable/index.html

How to do data field update based on value in a another field inside the SQL statement

I have the following table where I used to manage passwords to login.
Loging Table fields :
UserID, EmailAddress, PassWord, LoginAttempts, LastLoginDate, LoginStatus
In my login logic,
1. If login password matches :
1. I update LastLoginDate field with the current date and time
2. Update LoginAttempts = 0
2. If the EmailAddress (login user name) is correct but PassWord is wrong :
1. I increment LogginAttempts by 1
2. If LoginAttempts > 5 then update LoginStatus = "LOCKED"
3. No need to update anything if EmailAddress is wrong
Currently I read the table with the following query :
string Query = #"SELECT *,GROUP_CONCAT(user_role.USER_ROLE_ROLE SEPARATOR ',') AS ROLES " +
"FROM login " +
"INNER JOIN user_role ON " +
"user_role.USER_ROLE_USER_ID = login.LOGIN_USER_ID AND login.LOGIN_EMAIL = #Parameter1";
Then I compare the fields for correct PassWord and update the table with following SQL query :
Query = #"UPDATE login SET " +
"LOGIN_ACCOUNT_STATUS = #Parameter2," +
"LOGIN_LOGIN_ATTEMPTS = #Parameter3," +
"LOGIN_LAST_LOGIN_DATE = #Parameter4," +
"LOGIN_LAST_LOGIN_LOCATION = #Parameter5 " +
"WHERE LOGIN_EMAIL=#Parameter1";
This is implemented externally (comparing values outside of SQL) and working fine. But I am sure this is possible to implement within SQL statement.
Could someone help me with the SQL query need to implement?
I want to see how I could merge SELECT and UPDATE into one SQL field with comparisons within SQL query if possible. I am just trying to make the code elegant. My C# function is currently 100 lines of code and could be reduced significantly if the whole thing is implemented in SQL.
Thanks,
PG

SSRS - Using Parameters as part of a Dataset/Report Query

I am trying to run an SSRS report using the following Dataset Query:
SELECT timesheet_entry.ASSIGNMENT_ID, timesheet_entry.ENTRY_DATE, timesheet_entry.UPDATE_DATE, timesheet_entry.HOURS, timesheet_entry.COMMENT,
project.PROJECT_ID, project.NAME AS Name_Of_Project, project.DESCRIPTION, project.PROJECT_MANAGER, user_to_department.DEPARTMENT_ID,
user_to_department.USER_ID, users.USER_ID AS User_ID2, users.USERNAME, users.FIRST_NAME, users.LAST_NAME,
users.DEPARTMENT_ID AS DEPARTMENT_ID2, users.ACTIVE AS ACTIVE2, user_department.NAME, CONCAT(Users.LAST_NAME, ", ", Users.FIRST_NAME)
AS Full_Name, Customer.NAME AS Project_Cat
FROM user_department, user_to_department, project, Project_Assignment, Timesheet_entry, Users, Customer
WHERE (timesheet_entry.ENTRY_DATE BETWEEN (?) and (?)) AND (User_Department.NAME IN ('" & Join(Parameters!Parameter3.Value, "', '") & "')) AND
(user_department.DEPARTMENT_ID = user_to_department.DEPARTMENT_ID) AND (project.PROJECT_ID = project_assignment.PROJECT_ID) AND
(project_assignment.ASSIGNMENT_ID = timesheet_entry.ASSIGNMENT_ID) AND (project_assignment.USER_ID = users.USER_ID) AND
(users.USER_ID = user_to_department.USER_ID) AND (Project.Customer_ID = Customer.Customer_ID)
When I try to run the report, I receive all column headings, but no data. I know that the problem is my third '?', which is part of a text parameter that can have multiple values. When I make the query with that ? as a singular value, I get data back. Hoping someone can point out whether my syntax is wrong.
Also, I need to have this done within the query itself, utilizing it as a Filter in the dataset causes the report to run longer than desired.
Thank you in advance!

SQL checking if two tables have same data for a specific ID

Assume I have a table called Catalog like so:
Course Number CourseName CourseDsc PreReq
1028 CS146 Data S... 1005
1028 CS146 Data S... 2000
1028 CS146 Data S... 2003
and another table called PreviousCourses like so:
Username PrevCoursesID Grade
admin 1005 A
admin 2000 A
Now this user admin doesn't meet the pre-requisites for the course CS146 because he has two of the three pre requisites(1005 and 2000) but is missing the pre-requisite 2003. The way my query is set up is that its checking if PreviousCourses is a subset of Catalog. Basically if any of the pre-requisites are met for a certain courses, the user is allowed to enroll in that class. I want my query to behave that the user CAN ONLY enroll if ALL the prerequisites from the catalog for a certain course are met.
My Query Attempt:
"Select PrevCoursesID, Grade from PreviousCourses where "
+ "Grade <= 'D' AND "
+ "PrevCoursesID IN (Select PreReq from Catalog where CourseNumber = '" + courseID + "') AND"
+ " Username = '" + currentUser + "'";
update: Just tried this query and this doesn't seem to work either
SELECT CATALOG.prereq, previouscourses.grade
FROM CATALOG LEFT JOIN PREVIOUSCOURSES ON
CATALOG.prereq=PREVIOUSCOURSES.PREVCOURSESID AND
PREVIOUSCOURSES.GRADE<='D' WHERE PREVIOUSCOURSES.USERNAME='ADMIN'
AND CATALOG.COURSENUMBER='1028'
These are the classes a user can take:
DECLARE #UserName varchar(255) = 'admin'
SELECT c.[Course Number]
FROM Catalog c
GROUP BY c.[Course Number]
HAVING COUNT(1) = (SELECT COUNT(DISTINCT pc.PreReq)
FROM PreviousCourses pc
WHERE pc.Username = #UserName
AND c.PreReq = pc.PrevCoursesID
AND pc.Grade IN ('A', 'B', 'C', 'P', 'Pass')
OR MAX(ISNULL(c.PreReq, 0)) = 0
ORDER BY c.[Course Number]
(This is SQL Server)
Use the following query, then roll through the resulting recordset to check if any of the PreviousCourses.Grade values are NULL. If any are, the prereqs are not fully met.
Note that the LEFT OUTER JOIN combined with the absence of any sort of WHERE clause is guaranteeing that you'll get a row for every prereq, and by including the passing grade and user criteria in that JOIN criteria, you save yourself from having your check need to identify passing (thus only checking for NULL).
SELECT
Catalog.PreReq,
PreviousCourses.Grade
FROM
Catalog
LEFT OUTER JOIN PreviousCourses ON
Catalog.PreReq = PreviousCourses.PrevCoursesID
AND
PreviousCourses.Grade <='D'
AND
PreviousCourses.Username = 'admin'
Also, this is SQL Server syntax. You didn't tag the question with your actual db server, just sql. So if you're using mysql or sqlite or some other variant, this approach will still work, but the syntax may require tweaking.

VB.NET and MySQL query

How can i get values from mysql database usin while loop?
Sry for my bad english and i'm new in vb.net so please do not insult me because I do not know vb.net, I'm trying to learn, i'll try to explain what i need. :)
Lets go:
We know that EmailAddress is real email address, now first query must be something like this
Query1 = "select ID from database.table_users where email='" & EmailAddress & "'"
Now we know that Query1 will return value id from table_users.
Now we need create new query like this
Query2 = "select friend_id from database.table_friends where user_id='" & query1 & "'"
Okay, now Query2 return friend_id from table_friends.
One more query like this
Query3 = "select displayname from database.table_users where id='" & Query2 & "'"
Query3 must return displaynames of all users which we got in query2.
Example:
First i need query ID from table_users where email=EmailAddres
Then i need query friend_id from table_friends where user_id=ID
After that i nedd query displayname from table_users where id=friend_id
*these are just examples and they are not correct as you can see :D
I need 3 querys in while loop, and only last query (displayname) will go to the listbox. While must return all mysql values from table_friends where ID= id of EmailAddress user.
Join your tables into a single query. Something like this:
SELECT
Friends.displayname
FROM
table_users AS Users
INNER JOIN table_friends AS Connections
ON Users.user_id = Connections.user_id
INNER JOIN table_users AS Friends
ON Connections.friend_id = Friends.user_id
WHERE
Users.email = #EmailAddress
This creates two references to the table_users table, one for the initial user and one for the friends, connected by your many-to-many linking table. (Note also the use of a query parameter, which I highly recommend instead of directly concatenating input into the query.)
SQL is very powerful for querying structured data (hence its name), you'll be much better off building your query logic into SQL rather than keeping the SQL queries overly simple and making multiple trips to the database.
why loop and why can't you make it single query like below using JOIN
select tf.displayname from table_users tu
join table_friends tf on tu.id = tf.friend_id
and tu.email = '" & EmailAddress & "';