Edit (for clarification): Hannes wants to search records with a certain VID and associated records from the same table with the condition AID (from original records) = TID.
EDIT, because wrog problem description (sorry for that!)
I need a query in MySQL that loops through the selected rows and creates a result array. The pattern for this is shown below.
Currently I call a select again and again. Is there a way to create a single query from this? Thanks a lot!
Table structure
ID, VID, AID, ... (title, etc.) ... TID
SELECT id, vid, aid FROM tbl WHERE vid = 24;
ID VID AID
1 24 NULL
2 24 NULL
3 24 712
4 24 713
5 24 714
6 24 715
7 24 716
...
SELECT * FROM tbl WHERE TID = 112
SELECT * FROM tbl WHERE TID = 113
SELECT * FROM tbl WHERE TID = 114
SELECT * FROM tbl WHERE TID = 115
SELECT * FROM tbl WHERE TID = 116
ID VID AID ...
1 24 NULL
2 24 NULL
75 24 712
79 24 713
88 24 714
92 24 715
97 24 716
The problem in this e.g. is that i need a loop and tones of selects. Pseudocode:
rows = []
foreach (main_select)
{
if (aid != null)
{
selected_row = select * from tbl where tid = aid
rows[] = selected_row
}
else
{
rows[] = main_select_row_from_iteration
}
}
You should be able to do all of that with a single query
SELECT tbl1.ID as TBL1_ID, tbl1.VID as TBL1_VID, tbl1.AID as TBL1_AID,
tbl2.ID as TBL2_ID, tbl2.VID as TBL2_VID, tbl2.AID as TBL2_AID
FROM tbl AS tbl1
LEFT JOIN tbl AS tbl2 ON tbl1.AID = tbl2.TID
WHERE tbl1.VID = 24 AND tbl1.AID IS NOT NULL
It's a self-join of the same table, the WHERE condition makes sure that you have the initial VID and that there's not NULL value in AID. The JOIN takes AID and matches it to TID.
The fields in the SELECT list are from both, separated via alias.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.