-
Jul 28th, 2018, 01:55 PM
#1
Thread Starter
Lively Member
Mysql Query get name twise
Hi
someone who can help me with this Query
I need to get List from this Table but the problem is how can I get fiber name from Fiber twise in the list from this ID C_Fiber_INN_ID and C_Fiber_Out_ID
1 C001 INN A 1 OUT B 1
1 C001 INN A 2 OUT B 2
Fiber
--F_ID
--F_Name
--F_Nr
1 A 1
2 A 2
3 B 1
4 B 2
Connection
--C_ID
--C_Name
--C_In
--C_Fiber_INN_ID
--C_Out
--C_Fiber_Out_ID
1 C001 INN 1 OUT 3
2 C001 INN 2 OUT 4
-
Jul 28th, 2018, 09:38 PM
#2
Re: Mysql Query get name twise
If you're asking us how to write SQL for a MySQL database then why have you posted in the VB.NET forum? There's no connection to VB at all in your question. Even if the query will be executed from a VB app, the SQL would still be the same if the app was written in any other language. I've asked the mods to move this thread to the Database Development forum.
As for the question, you simply join the same table twice, using an alias for each one, and then use the alias to refer to that instance of the table in the rest of the code. I don't really use MySQL so I'm not exactly sure how it uses aliases but I would assume that it is similar to SQL Server where you simply put the alias immediate after the table name, e.g. "INNER JOIN SomeTable SomeAlias", or use the 'AS' keyword, e.g. "INNER JOIN SomeTable AS SomeAlias". I'm sure that you can find out easily enough with a web search. If I'm reading your question correctly then you would need something like this:
SQL Code:
SELECT c.C_ID,
c.C_Name,
c.C_In,
fin.F_Naame,
c.C_Out,
fout.F_Name
FROM Connection c INNER JOIN Fiber fin
ON c.C_Fiber_INN_ID = fin.F_ID INNER JOIN Fiber fout
ON c.C_Fiber_Out_ID = fout.F_ID
Notice that I also aliased the Connection table even though I didn't have to. You may or may not choose to do that but I tend to alias every table in a SQL query pretty much every time because it makes referring to them easier in the rest of the code, e.g. I didn't have to put "Connection" over and over in the SELECT clause.
-
Jul 29th, 2018, 04:22 AM
#3
Re: Mysql Query get name twise
Thread moved to the 'Database Development' forum - which is where you should always post SQL questions
-
Jul 29th, 2018, 10:21 AM
#4
Thread Starter
Lively Member
Re: Mysql Query get name twise
-
Jul 29th, 2018, 06:30 PM
#5
Re: Mysql Query get name twise
Originally Posted by jmcilhinney
I didn't have to put "Connection" over and over in the SELECT clause.
I guess I out to also point out that I didn't need to refer to the tables at all in the SELECT clause because the column names weren't ambiguous so, from that perspective, I could have not aliased Connection and still not had to refer to the table repeatedly but I think that the table references in the SELECT clause do add some clarity.
-
Jul 29th, 2018, 10:16 PM
#6
Re: Mysql Query get name twise
Originally Posted by jmcilhinney
I guess I out to also point out that I didn't need to refer to the tables at all in the SELECT clause because the column names weren't ambiguous so, from that perspective, I could have not aliased Connection and still not had to refer to the table repeatedly but I think that the table references in the SELECT clause do add some clarity.
I find that as soon as I alias one table, I alias them all, and I always use the alias name for the columns even if it's not ambiguous... habits formed on small queries will help on larger queries, and nothing is more frustrating that trying to find out which of the dozen tables that's been joined is the owner of a particular column.
-tg
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|