VB.NET and MySQL query - mysql

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 & "';

Related

DLookUp query to MySql

I am working on converting a legacy MS access system to a spring-boot application and I came across a big query. I am mostly done with converting the functions from access to mysql but not able to understand how to convert the following DLookUp sub-query as a mySql subquery
DLookUp("[price]","tbl_so","[so_id] = " & tbl_trade.so_id & " AND [product_id] = " & tbl_po.product_id
What I understood is following won't work as I don't have the Inner Joins set between the 3 tables, tbl_so, tbl_po, tbl_trade
SELECT tbl_so.price FROM tbl_so WHERE tbl_so.so_id = tbl_trade.so_id AND tbl_so.product_id = tbl_po.product_id
My question is how do I know how the tables will join with each other in this case and also when this DLookUp query is seldom used. And also the solution for this query.
Well, as a general rule, dlookup() can be replaced with a left join. However, you can also use a sub-query and they tend to be "less" change to the query.
So, if we had this:
SELECT id, partNum, dlookup("PartDescrt","tblParts","PartID = " & partNum)
as Description from tblOrders.
You would replace the above dlookup() with a sub-query like this:
SELECT id, partNum,
(select PartDescrt from tblParts where tblParts.PartID = tblOrders.PartNum)
AS Description
from tblOrders
The above is SQL or access sql syntax, but quite sure the same should work for MySQL.
If there is more then one partNumber that matches the above, then add a TOP 1, and a order by with some unique row (PK ID column is best). So the query becomes:
(select TOP 1 PartDescrt from tblParts where tblParts.PartID = tblOrders.PartNum
ORDER BY PartDescrt, ID DESC)
AS Description

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!

visual studio and sql how to select multiple tables

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'
...

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.

SELECT FROM 2 different tables using INNER JOIN

Last time I was able to combined 3 different SELECT queries since it was from the same table. Now, I'm trying to execute a query where in the info was from 2 different tables.
Here's my query string
SELECT applicantinfo.FirstName,
applicantinfo.MiddleName,
applicantinfo.LastName,
applicantaccess.ApplicantExamPassword
FROM applicantinfo
LEFT JOIN applicantaccess WHERE applicantaccess.ApplicantID = '" & lblID.Text & "'"
using myphpadmin to test this query and replacing the lblID.text with a value, Instead of showing up a singe result, it shows 2 rows.
Here's what it looks like
I think I miss used using the INNER JOIN keyword here.
My expected output should only be the first row.
*note
Jaranilla's ID should be '201458971' and password is 6zo93ie82m
lopez's ID should be '201437095' and password is 4ew93fo86t
You have to specify which properties constitute the join, assuming that "id" is the common attribute, e.g.
SELECT applicantinfo.FirstName, applicantaccess.ApplicantExamPassword FROM applicantinfo LEFT JOIN applicantaccess ON applicantinfo.id = applicantaccess.id;
In join put condition on which you want to join both tables, try following appID is you primary key and fk_appID is foregin key.
SELECT applicantinfo.FirstName, applicantinfo.MiddleName, applicantinfo.LastName, applicantaccess.ApplicantExamPassword
FROM applicantinfo as ainfo
LEFT JOIN applicantaccess as aa on ainfo.appID = aa.fk_appID
WHERE applicantaccess.ApplicantID = '" & lblID.Text & "'"