gpt4 book ai didi

Mysql如何获取子查询的列名?

转载 作者:行者123 更新时间:2023-11-29 20:13:38 24 4
gpt4 key购买 nike

假设我有以下查询:

SELECT
c.id AS 'course_id',u.id as 'user_id',c.round,u.first_name as name,u.last_name as family_name,u.`father_name`,u.`tazkera_id` as tazkira,c.`no` as class_ID,tc.province,tc.village,tl.id as training_provider_id,
CASE c.`shift`
WHEN 1 THEN 'Morning'
WHEN 2 THEN 'Afternoon'
END as shift,
c.`start_date`as start_date,
sa.participant_id,
DATE_FORMAT(ca.`date`,'%Y-%m-%d') as attendence,
CASE sa.`precence`
WHEN 0 THEN 'A'
WHEN 1 THEN 'P'
END as attendence_status
FROM `users` AS u
LEFT JOIN `provinces` as pu On pu.`province_code` = u.`province_code`
LEFT JOIN `districts` as du on du.`district_code` = u.`district_code`

LEFT JOIN `course_user` as cu ON cu.`user_id` = u.id AND cu.`relation_type` = 1
LEFT JOIN `courses` AS c On c.id = cu.`course_id`


LEFT JOIN `training_centers` as tc On tc.id = c.`training_center_id`
LEFT JOIN `training_providers` AS tl ON tl.id = tc.`training_provider_id`
/*** Training location **/
LEFT JOIN `provinces` as pt On pt.`province_code` = tc.`province`
LEFT JOIN `districts` as dt on dt.`district_code` = tc.`district`
LEFT JOIN `course_attendances` as ca ON ca.`course_id` = c.`id`
LEFT JOIN `student_attendances` as sa ON sa.`course_attendance_id` = ca.`id`

WHERE c.id IS NOT NULL AND sa.`participant_id` = u.id
GROUP BY sa.`participant_id`,ca.date order by u.id,ca.date

我想用 mysql 获取上述查询的列名。当然,使用 php 我们可以做到这一点,但我的问题是,如何使用 mysql 查询做到这一点?这可能吗?

最佳答案

MySQL 只是数据库系统。你告诉它选择什么,它就会这样做。在您的情况下,您告诉它选择 courses.id 并将其命名为 course_id 等等。列名称隐含在结果中。其工作原理如下:您的应用程序将查询发送到 MySQL; MySQL 会检查它并告诉您是否正常。您的应用程序需要列; MySQL 告诉您列名和类型。您的应用程序要求提供行; MySQL 发送它们。

您通常会使用自动执行所有操作的应用程序,例如 phpMyAdmin。当您自己编写这样的应用程序时,该语言通常会对您隐藏所有这些(例如 Java 框架或 .NET)。但当然,您也可以明确询问列名称和类型,例如在 PHP 中:

mysqli_fetch_field_direct($result, $column_index)

只需搜索“MySQL”+<相关语言>+“列名称”,您可能会找到有关如何执行此操作的大量信息。

关于Mysql如何获取子查询的列名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39930986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com