简体   繁体   English

VB.NET和MySQL查询

[英]VB.NET and MySQL query

How can i get values from mysql database usin while loop? 我如何在while循环中从mysql数据库中获取值?

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. 对不起,我的英语不好,我在vb.net中是新手,所以请不要侮辱我,因为我不知道vb.net,我在努力学习,我将尽力解释我的需要。 :) :)

Lets go: 我们走吧:

We know that EmailAddress is real email address, now first query must be something like this 我们知道EmailAddress是真实的电子邮件地址,现在第一个查询必须是这样的

Query1 = "select ID from database.table_users where email='" & EmailAddress & "'"

Now we know that Query1 will return value id from table_users . 现在我们知道Query1将返回table_users值id。 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 . 好了,现在Query2返回friend_idtable_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 . Query3必须返回displaynames我们将在得到所有用户的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 首先,我需要query ID from table_users where email=EmailAddres然后我需要query friend_id from table_friends where user_id=ID之后,我需要query displayname from table_users where id=friend_id

*these are just examples and they are not correct as you can see :D *这些只是示例,如您所见,它们是不正确的:D

I need 3 querys in while loop, and only last query (displayname) will go to the listbox. 我在while循环中需要3个查询,并且只有最后一个查询(显示名称)会转到列表框。 While must return all mysql values from table_friends where ID= id of EmailAddress user. While必须从table_friends返回所有mysql值,其中ID = EmailAddress用户的ID。

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. 这将创建对table_users表的两个引用,一个对初始用户,对一个朋友,通过多对多链接表连接。 (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. SQL在查询结构化数据(因此得名)方面非常强大 ,因此将查询逻辑构建到SQL中要比将SQL查询过分简单和多次访问数据库要好得多。

why loop and why can't you make it single query like below using JOIN 为什么循环以及为什么不能像下面那样使用JOIN使其成为单个查询

select tf.displayname from table_users tu
join table_friends tf on tu.id = tf.friend_id
and tu.email = '" & EmailAddress & "';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM