나는 이것이 왜 필요한지 완전히 이해합니다. 적어도 나에게는 많은 내부 조인을 포함하여 많은 테이블을 조인해야 할 때 신속한 프로토 타이핑 중에 편리합니다. 두 번째 "joinedtable. *"필드 와일드 카드에서 열 이름이 동일하면 기본 테이블의 필드 값이 결합 가능한 값으로 대체됩니다. 별칭이 여러 번인 테이블 필드를 수동으로 지정해야 할 경우 오류가 발생하기 쉽고 좌절하며 DRY 위반이 발생합니다.
다음은 코드 생성을 통해이를 사용하는 예제와 함께 PHP (Wordpress) 함수입니다. 이 예에서는 고급 사용자 정의 필드 필드를 통해 참조 된 관련 워드 프레스 게시물의 필드를 제공하는 사용자 정의 쿼리를 신속하게 생성하는 데 사용됩니다 .
function prefixed_table_fields_wildcard($table, $alias)
{
global $wpdb;
$columns = $wpdb->get_results("SHOW COLUMNS FROM $table", ARRAY_A);
$field_names = array();
foreach ($columns as $column)
{
$field_names[] = $column["Field"];
}
$prefixed = array();
foreach ($field_names as $field_name)
{
$prefixed[] = "`{$alias}`.`{$field_name}` AS `{$alias}.{$field_name}`";
}
return implode(", ", $prefixed);
}
function test_prefixed_table_fields_wildcard()
{
global $wpdb;
$query = "
SELECT
" . prefixed_table_fields_wildcard($wpdb->posts, 'campaigns') . ",
" . prefixed_table_fields_wildcard($wpdb->posts, 'venues') . "
FROM $wpdb->posts AS campaigns
LEFT JOIN $wpdb->postmeta meta1 ON (meta1.meta_key = 'venue' AND campaigns.ID = meta1.post_id)
LEFT JOIN $wpdb->posts venues ON (venues.post_status = 'publish' AND venues.post_type = 'venue' AND venues.ID = meta1.meta_value)
WHERE 1
AND campaigns.post_status = 'publish'
AND campaigns.post_type = 'campaign'
LIMIT 1
";
echo "<pre>$query</pre>";
$posts = $wpdb->get_results($query, OBJECT);
echo "<pre>";
print_r($posts);
echo "</pre>";
}
출력 :
SELECT
`campaigns`.`ID` AS `campaigns.ID`, `campaigns`.`post_author` AS `campaigns.post_author`, `campaigns`.`post_date` AS `campaigns.post_date`, `campaigns`.`post_date_gmt` AS `campaigns.post_date_gmt`, `campaigns`.`post_content` AS `campaigns.post_content`, `campaigns`.`post_title` AS `campaigns.post_title`, `campaigns`.`post_excerpt` AS `campaigns.post_excerpt`, `campaigns`.`post_status` AS `campaigns.post_status`, `campaigns`.`comment_status` AS `campaigns.comment_status`, `campaigns`.`ping_status` AS `campaigns.ping_status`, `campaigns`.`post_password` AS `campaigns.post_password`, `campaigns`.`post_name` AS `campaigns.post_name`, `campaigns`.`to_ping` AS `campaigns.to_ping`, `campaigns`.`pinged` AS `campaigns.pinged`, `campaigns`.`post_modified` AS `campaigns.post_modified`, `campaigns`.`post_modified_gmt` AS `campaigns.post_modified_gmt`, `campaigns`.`post_content_filtered` AS `campaigns.post_content_filtered`, `campaigns`.`post_parent` AS `campaigns.post_parent`, `campaigns`.`guid` AS `campaigns.guid`, `campaigns`.`menu_order` AS `campaigns.menu_order`, `campaigns`.`post_type` AS `campaigns.post_type`, `campaigns`.`post_mime_type` AS `campaigns.post_mime_type`, `campaigns`.`comment_count` AS `campaigns.comment_count`,
`venues`.`ID` AS `venues.ID`, `venues`.`post_author` AS `venues.post_author`, `venues`.`post_date` AS `venues.post_date`, `venues`.`post_date_gmt` AS `venues.post_date_gmt`, `venues`.`post_content` AS `venues.post_content`, `venues`.`post_title` AS `venues.post_title`, `venues`.`post_excerpt` AS `venues.post_excerpt`, `venues`.`post_status` AS `venues.post_status`, `venues`.`comment_status` AS `venues.comment_status`, `venues`.`ping_status` AS `venues.ping_status`, `venues`.`post_password` AS `venues.post_password`, `venues`.`post_name` AS `venues.post_name`, `venues`.`to_ping` AS `venues.to_ping`, `venues`.`pinged` AS `venues.pinged`, `venues`.`post_modified` AS `venues.post_modified`, `venues`.`post_modified_gmt` AS `venues.post_modified_gmt`, `venues`.`post_content_filtered` AS `venues.post_content_filtered`, `venues`.`post_parent` AS `venues.post_parent`, `venues`.`guid` AS `venues.guid`, `venues`.`menu_order` AS `venues.menu_order`, `venues`.`post_type` AS `venues.post_type`, `venues`.`post_mime_type` AS `venues.post_mime_type`, `venues`.`comment_count` AS `venues.comment_count`
FROM wp_posts AS campaigns
LEFT JOIN wp_postmeta meta1 ON (meta1.meta_key = 'venue' AND campaigns.ID = meta1.post_id)
LEFT JOIN wp_posts venues ON (venues.post_status = 'publish' AND venues.post_type = 'venue' AND venues.ID = meta1.meta_value)
WHERE 1
AND campaigns.post_status = 'publish'
AND campaigns.post_type = 'campaign'
LIMIT 1
Array
(
[0] => stdClass Object
(
[campaigns.ID] => 33
[campaigns.post_author] => 2
[campaigns.post_date] => 2012-01-16 19:19:10
[campaigns.post_date_gmt] => 2012-01-16 19:19:10
[campaigns.post_content] => Lorem ipsum
[campaigns.post_title] => Lorem ipsum
[campaigns.post_excerpt] =>
[campaigns.post_status] => publish
[campaigns.comment_status] => closed
[campaigns.ping_status] => closed
[campaigns.post_password] =>
[campaigns.post_name] => lorem-ipsum
[campaigns.to_ping] =>
[campaigns.pinged] =>
[campaigns.post_modified] => 2012-01-16 21:01:55
[campaigns.post_modified_gmt] => 2012-01-16 21:01:55
[campaigns.post_content_filtered] =>
[campaigns.post_parent] => 0
[campaigns.guid] => http://example.com/?p=33
[campaigns.menu_order] => 0
[campaigns.post_type] => campaign
[campaigns.post_mime_type] =>
[campaigns.comment_count] => 0
[venues.ID] => 84
[venues.post_author] => 2
[venues.post_date] => 2012-01-16 20:12:05
[venues.post_date_gmt] => 2012-01-16 20:12:05
[venues.post_content] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
[venues.post_title] => Lorem ipsum venue
[venues.post_excerpt] =>
[venues.post_status] => publish
[venues.comment_status] => closed
[venues.ping_status] => closed
[venues.post_password] =>
[venues.post_name] => lorem-ipsum-venue
[venues.to_ping] =>
[venues.pinged] =>
[venues.post_modified] => 2012-01-16 20:53:37
[venues.post_modified_gmt] => 2012-01-16 20:53:37
[venues.post_content_filtered] =>
[venues.post_parent] => 0
[venues.guid] => http://example.com/?p=84
[venues.menu_order] => 0
[venues.post_type] => venue
[venues.post_mime_type] =>
[venues.comment_count] => 0
)
)