I have code:
$query = "SELECT a.*, c.name as categoryname, c.id as categoryid
FROM #__table_one as a
LEFT JOIN #__table_two c ON c.id = a.catid";
$query .= " WHERE a.published = 1
AND a.access <= {$aid}
AND a.trash = 0
AND c.published =
AND c.access <= {$aid}
AND c.trash = 0";
I would like to add a third table ('__some_table') for the parts of the query where a.publish, a.access and a.trash. In other words, I want these fields to be retrieved from another table, not "#__table_one", but I do not know how to incorporate the #__some_table into the current query
I imagine the JOIN command can help me, but I do not know how to code mysql
//not tested
$query = "SELECT a.*, c.name as categoryname, c.id as categoryid
FROM #__table_one as a
LEFT JOIN #__table_two c ON c.id = a.catid
LEFT JOIN #__table_three d ON d.id = a.some_id";
Related
I am new in code igniter. I am trying to execute delete query using joining multiple tables, but I am getting error in query.
Here is my query code.
$this->db->from('order');
$this->db->join('item_order', 'item_order.order_id = order.order_id','left');
$this->db->join('product', 'product.product_number = item_order.item_number','left');
$this->db->join('product_to_image', 'product_to_image.p_id = product.products_id','left');
$this->db->join('product_to_dropbox', 'product_to_dropbox.products_id = product.products_id','left');
$this->db->where('order.user_name', $ebay_user_name);
$this->db->where('order.user_id', $user_id);
$this->db->delete('order');
When using Codeigniter you can not perform a DELETE with Join. see here and here.
You need to write a natural SQL. See link 1:
$sql = "DELETE o FROM order as o
LEFT JOIN item_order ON item_order.order_id = order.order_id
LEFT JOIN product ON product.product_number = item_order.item_number
LEFT JOIN product_to_image ON product_to_image.p_id = product.products_id
LEFT JOIN product_to_dropbox ON product_to_dropbox.products_id = product.products_id
WHERE order.user_name = ? and order.user_id = ?";
$this->db->query($sql, array($ebay_user_name, $user_id));
OR try with backticks `. But I'm not sure whether or not escaping them:
$sql = "DELETE o FROM `order` as o
LEFT JOIN `item_order` ON item_order.order_id = order.order_id
LEFT JOIN `product` ON product.product_number = item_order.item_number
LEFT JOIN `product_to_image` ON product_to_image.p_id = product.products_id
LEFT JOIN `product_to_dropbox` ON product_to_dropbox.products_id = product.products_id
WHERE order.user_name = ? and order.user_id = ?";
$this->db->query($sql, array($ebay_user_name, $user_id));
I use code below to get data from database
if( !empty( $books_ids ) )
{
$books_ids_in = implode(',', array_fill(0, count($books_ids), '?'));
$query = "SELECT
b.id,
b.`name`,
b.`year`,
GROUP_CONCAT(DISTINCT a.`name`) AS author_names,
GROUP_CONCAT(DISTINCT s.`name`) AS store_names
FROM
books AS b
LEFT JOIN books_authors AS b_a ON b.id = b_a.book_id
LEFT JOIN authors AS a ON a.id = b_a.author_id
LEFT JOIN books_stores AS b_s ON b.id = b_s.book_id
LEFT JOIN stores AS s ON s.id = b_s.store_id
WHERE
b.id IN (". $books_ids_in .")
GROUP BY b.id
ORDER BY b.id";
$stmt = $conn->prepare($query);
foreach ($books_ids as $k => $id) {
$stmt->bindValue(($k+1), $id);
}
$stmt->execute();
$results = $stmt->fetchAll();
}
and as I use $book id for this purpose, I would like to add some parameter in result to show that, for example, param = "book" for every row. Is there any way to do that?
Just pass it the string and alias it as a column. Though since you know the value passed in in code and display the values though code...... I'm not sure why you need this... as the value is available to you in the code when being displayed.
$query = "SELECT
b.id,
b.`name`,
b.`year`,
GROUP_CONCAT(DISTINCT a.`name`) AS author_names,
GROUP_CONCAT(DISTINCT s.`name`) AS store_names,
'". $param."' as forEveryRow
FROM
books AS b
LEFT JOIN books_authors AS b_a ON b.id = b_a.book_id
LEFT JOIN authors AS a ON a.id = b_a.author_id
LEFT JOIN books_stores AS b_s ON b.id = b_s.book_id
LEFT JOIN stores AS s ON s.id = b_s.store_id
WHERE
b.id IN (". $books_ids_in .")
GROUP BY b.id
ORDER BY b.id";
I am trying to execute this query to print data from three tables. But somehow the query is not being executed.
SELECT l.locality_id, c.city_name, l.locality_name, cloud_site_location_outlet_localities.minimum_order, cloud_site_location_outlet_localities.delivery_time, cloud_site_location_outlet_localities.delivery_charge FROM `cloud_site_location_localities` l INNER JOIN `cloud_site_location_cities` c on l.city_id=c.city_id
The error that I get on executing above query is:
1054 - Unknown column 'cloud_site_location_outlet_localities.minimum_order' in 'field list'
However this query worked fine when I had to display data from two tables only.
SELECT l.locality_id, c.city_name, l.locality_name FROM `cloud_site_location_localities` l INNER JOIN `cloud_site_location_cities` c on l.city_id=c.city_id
Where am I going wrong?
When I use the following query to export it in CSV, I get redundant data. Out of 1000 entries, I only get 400 and then those 400 are repeated.
This is code I am implementing
$sql = "SELECT l.locality_id, c.city_name, l.locality_name, o.minimum_order, o.delivery_time, o.delivery_charge FROM `cloud_site_location_localities` l INNER JOIN `cloud_site_location_cities` c ON l.city_id = c.city_id INNER JOIN `cloud_site_location_outlet_localities` o ON l.locality_id = o.locality_id";
$result = $DB->query($sql);
//$output = fopen($filename, 'c+');
$output = fopen('php://output', 'c+');
fputcsv($output, array('Location Id', 'City Name', 'Location Name','Minimum Order Amount','Delivery Time','Delivery Charge'));
$row = array();
$csv_output = "";
$csv_output .= "Locality Id,City Name,Locality Name,Minimum Order Amount,Delivery Time,Delivery Charge\n";
foreach($result as $locality_value)
{
$row['locality_id'] = $locality_value->locality_id;
$row['city_name'] = $locality_value->city_name;
$row['locality_name'] = $locality_value->locality_name;
$row['minimum_order'] = $locality_value->minimum_order;
$row['delivery_time'] = $locality_value->delivery_time;
$row['delivery_charge'] = $locality_value->delivery_charge;
$csv_output .= trim($row['locality_id']).",".trim($row['city_name']).",".trim($row['locality_name']).",".trim($row['minimum_order']).",".trim($row['delivery_time']).",".trim($row['delivery_charge']).",,\n";
fputcsv($output, $row);
}
fclose($output);
print $csv_output;
You are missing the join clause for cloud_site_location_outlet_localities. E.g.:
SELECT l.locality_id,
c.city_name,
l.locality_name,
o.minimum_order,
o.delivery_time,
o.delivery_charge
FROM `cloud_site_location_localities` l
INNER JOIN `cloud_site_location_cities` c
ON l.city_id = c.city_id
INNER JOIN `cloud_site_location_outlet_localities` o
ON l.outlet_id = c.outlet_id --just guessing the column names
simple structure for joining three tables in mysql
select *
from
tbA a
inner join
tbB b
on a.common = b.common
inner join
tbC c
on b.common = c.common
I'm not sure why this isn't working. If I join either table separately, it comes back with the appropriate results, but when I try to join them both, I get 0 results. (car_id and boat_id are both primary keys on their tables.)
$query = "SELECT
*
FROM
posted c
JOIN posted_car e on c.car_id = e.car_id
JOIN posted_boat g on c.boat_id = g.boat_id
WHERE
c.posted = 'posted'
ORDER BY date DESC LIMIT 0, 30";
$resultBoth = mysql_query($query, $db) or die(mysql_error($db));
Might be worth noting that when I do
LEFT JOIN posted_car e on c.car_id = e.car_id
RIGHT JOIN posted_boat g on c.boat_id = g.boat_id
I get results as if I had only joined the posted_boat table. If anyone could point me in the right direction...it would be much appreciated.
you are using JOIN that might be a problem . you should use left outer join to get proper result . check following syntax :
$query = "SELECT *
FROM
posted c
left OUTER JOIN posted_car e on c.car_id = e.car_id
left OUTER JOIN posted_boat g on c.boat_id = g.boat_id
WHERE c.posted = 'posted'
ORDER BY date DESC LIMIT 0, 30";
$resultBoth = mysql_query($query, $db) or die(mysql_error($db));
I have a mysql table jobs.
This is the basic structure of jobs.
id
booked_user_id
assigned_user_id
I then also have another table, meta.
Meta has the structure:
id
user_id
first_name
last_name
Here is my php code
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS job_id, job_name, priority_id, meta.first_name, date_booked
FROM jobs
LEFT JOIN (meta) on (meta.user_id = jobs.booked_user_id)
LEFT JOIN (jobs_priorities) on (jobs_priorities.id = jobs.priority_id)
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query($sQuery);
while ( $aRow = mysql_fetch_assoc( $rResult ) )
{
$sOutput .= '"'.addslashes($aRow['job_id']).'",';
}
How can I join these tables so that both booked_user_id and assigned_user_id can access meta.first_name?
When I try
$sOutput .= '"'.addslashes($aRow['first_name']).'",
nothing happens
Thanks for your advice
Tim
You can join twice:
SELECT j.id, b.first_name, a.first_name
FROM jobs j
JOIN meta b ON j.booked_user_id = b.user_id
JOIN meta a ON j.assigned_user_id = a.user_id
Nathan did the fix, but will apply it to your current SQL so you can understand it more
Lets transform your query into this:
SELECT SQL_CALC_FOUND_ROWS job_id, job_name, priority_id, date_booked
FROM jobs j
LEFT JOIN meta b ON b.user_id = j.booked_user_id
LEFT JOIN meta a ON a.user_id = j.assigned_user_id
LEFT JOIN jobs_priorities jp ON jp.id = j.priority_id
$sWhere
$sOrder
$sLimit
What I did is to use alias to method and join twice the meta, (just like what nathan did), I temporarily removed the first_name field,
Then let's add something on the SELECT so you can display both first_name
SELECT SQL_CALC_FOUND_ROWS job_id, job_name, priority_id, date_booked, b.first_name as booked_first_name, a.first_name as assigned_first_name
FROM jobs j
LEFT JOIN meta b ON b.user_id = j.booked_user_id
LEFT JOIN meta a ON a.user_id = j.assigned_user_id
LEFT JOIN jobs_priorities jp ON jp.id = j.priority_id
$sWhere
$sOrder
$sLimit
Now, we added the column booked_first_name and assigned_first_name, now you can call it on your php code like this:
$aRow['booked_first_name'] or $aRow['assigned_first_name']