Inner Join Query - 3 tables - mysql

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

Related

Left join is not working on codeigniter

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));

pdo add value to sql result

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";

MySQL - alternative for like - regexp?

i have got over one milion record in my database table.
When I use like is very slowly, when i use match against they lost some records.
I create help table:
tag_list
tag_id
tag_name
tag_rel_message
tag_id
messag_id
messages
message_id
message_text
in tag_list i add all words of $message_text - explode(" ", $message_text);
My new query is:
SELECT m.*
FROM tag_rel_messages trm
INNER JOIN messages m ON (trm.message_id = m.message_id)
INNER JOIN tag_list tl ON (trm.tag_id=tl.tag_id
WHERE tl.tag_name REGEXP 'pionas' AND tl.tag_name REGEXP 'website'
GROUP By trm.message_id
But not display any records.
What's wrong with this query?
Maybe i should use something other than REGEXP?
Thanks for help
I'm not sure how one column could match two things at the same time! You can use OR in place of AND, or you can use a single call to REGEXP.
SELECT m.*
FROM tag_rel_messages trm
INNER JOIN messages m ON (trm.message_id = m.message_id)
INNER JOIN tag_list tl ON (trm.tag_id=tl.tag_id
WHERE tl.tag_name REGEXP 'pionas|website'
GROUP By trm.message_id
You need to join with the tag_list and tag_rel_message tables twice to match two different tags.
SELECT m.*
FROM messages AS m
JOIN tag_rel_messages AS trm1 ON m.message_id = trm1.message_id
JOIN tag_list AS t1 ON t1.tag_id = trm1.tag_id
JOIN tag_rel_messages AS trm2 ON m.message_id = trm2.message_id
JOIN tag_list AS t2 on t2.tag_id = trm2.tag_id
WHERE t1.tag_name = 'pionas' AND t2.tag_name = 'website'
Another way to do it is to join with either tag, and count the number of results per message
SELECT m.*
FROM messages AS m
JOIN tag_rel_messages AS trm ON m.message_id = trm.message_id
JOIN tag_list AS t ON t1.tag_id = trm.tag_id
WHERE t.tag_name IN ('pionas', 'website')
GROUP BY m.message_id
HAVING COUNT(*) = 2
This second form is a little easier to generalize to any number of tags. Put all the tags in the IN clause, and change the HAVING clause to match the number of tags.
$words = array('pionas', 'website');
$tags_id = array();
foreach ($words as $key => $val) {
$sql = "SELECT tag_id FROM tag_list WHERE tag_name LIKE '%".$val."%'";
$records = $db->query($sql);
$tags_id[$key] = array_column($records, "tag_id");
}
$inner_array = array();
foreach ($tags_id as $k => $v) {
$short_name = "trm_".$k;
$inner_array[] = " INNER JOIN tag_rel_message as ".$short_name." ON ( ".$short_name.".message_id = m.message_id AND ".$short_name.".tag_id IN (".implode(", ", $v).")) ";
}
$sql = "SELECT DISTINCT m.* FROM messages m".implode(" ", $inner_array);
I think this is the same:
SELECT * FROM messages WHERE message_text like '%pionas%' AND message_text like '%website%'

MySQL JOIN Multiple tables not working

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));

mysql syntax how to add a third table to $query

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";