SQL is not my strength and i am trying to get values from different tables in the WordPress Database, i need to get user and user_mail from wp_user, date and orderid from wp_wc_lottery_log and meta_key, meta_value for billing_address_1, billing_first_name, billing_last_name, billing_phone from wp_usermeta. This is the code i have but i cannot get to display those meta_values in the corresponding meta_key, is there something wrong with my Query? Thanks!
function get_data_lotto( $args ) {
global $wpdb;
$results = $wpdb->get_results( "SELECT user_login,date,orderid,user_email,meta_key,meta_value FROM wp_wc_lottery_log INNER JOIN wp_users ON wp_wc_lottery_log.userid=wp_users.id INNER JOIN wp_usermeta ON wp_wc_lottery_log.userid=wp_usermeta.user_id WHERE lottery_id=1161" );
$content = '<table id="tabla-rifa" class="cell-border compact stripe" data-page-length="100">';
$content .= '<thead><tr><th>ID Ticket</th><th>Usuario</th><th>E-Mail</th><th>Fecha y Hora</th><th>Nº de orden</th><th>Nombre</th><th>Dirección</th><th>Teléfono</th></tr></thead><tbody>';
$count = 1;
foreach ( $results AS $result ) {
$content .= '<tr>';
$content .= '<td>' . $count . '</td>';
$content .= '<td>' . $result->user_login . '</td>';
$content .= '<td>' . $result->user_email . '</td>';
$content .= '<td>' . $result->date . '</td>';
$content .= '<td>' . $result->orderid . '</td>';
//ADD META_VALUE AND META_KEY
$content .= '</tr>';
$count++;
}
$content .= '</tbody></table>';
return $content;
}
2
Answers
There was some inconsistency in your detail.
Your basic form is reasonable, but you were missing criteria for the
meta_keys
:You may need to also recheck your spelling and actual columns in the tables.
The description mentioned columns to return, but not about the rest of the columns needed by the query.
Provide all columns in each table needed for your query, not just those to be returned.
Add that detail to your question.
Based on what I think you mean. Try updating your query to this:
When you’re performing joins, you have to scope out the tables in each statement, so MYSQL knows which table each field is from.