简体   繁体   中英

How can I CONCAT fields from different independent tables in MySQL?

I'm getting form data and using it to fetch them individually and then concat. My current query is this:

$design_code = $_POST['design_num'];
$design_code .= ':'.$db->single('SELECT code FROM '. table_brands .' WHERE id=?', [$_POST['brand']]);
$design_code .= ':'.$db->single('SELECT code FROM '. table_types .' WHERE id=?', [$_POST['type']]);
$design_code .= ':'.$db->single('SELECT code FROM '. table_quality .' WHERE id=?', [$_POST['quality']]);
$design_code .= ':'.$db->single('SELECT code FROM '. table_sizes .' WHERE id=?', [$_POST['size']]);

All the POST data are independent and hence cannot be joined. What can be a proper replacement query to merge them in one.

PS: $db->single() basically sends one single field value.

Try this

select (select code FROM  table1 where id = 1 ) as code1, (select code FROM  table2 where id = 1 ) as code2, table3.code3 FROM table3 where id = 1

The result will get in a single row.

And using UNION query the result will come in more than one row.

EDIT

Update query as per your last comment "Please check: pastebin.com/qGmp8ZKq"

select (select code FROM  brand where id = 2 ) as code1, type.code FROM type where id = 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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