If I have 15 rows in one table and i join another table to it, can i still output all 15 even if i want to join where the value could be null? Basically can you JOIN + include nulls from the result of the join. For example:
1
2
3
4
5
6
After join only 3 have the join parameter
2
4
5
How can i display everything?
I tried the following:
"SELECT *
FROM `ultrait_wpl_properties`
JOIN ultrait_wpl_property_types
ON ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
ORDER BY ultrait_wpl_properties.id"
This part outputs duplicate IDs?
$sql = "SELECT *
FROM `ultrait_wpl_properties`
LEFT JOIN ultrait_wpl_property_types
ON ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
ORDER BY ultrait_wpl_properties.id ASC";
$result = mysqli_query($con, $sql); // Connect and run query
$dom = new DOMDocument(); // New DOM
$root = $dom->createElement('root'); // Create parent or root node
$dom->appendChild($root); // Append the root tag to the DOM
while($r = mysqli_fetch_assoc($result)){
$node = $textContent = null; // $node will equal $textContent which is null
$property = $dom->createElement('property'); // Create containing node
foreach($r as $column_name => $val) { // Loop through key value pairs
// so loop all the values on each row
Try these:
"SELECT *
FROM `ultrait_wpl_properties`
LEFT JOIN ultrait_wpl_property_types
ON ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
ORDER BY ultrait_wpl_properties.id"
OR:
"SELECT *
FROM `ultrait_wpl_properties`
RIGHT JOIN ultrait_wpl_property_types
ON ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
ORDER BY ultrait_wpl_properties.id"
shree.pat18 is correct it either depends on your situation to use INNER, RIGHT OR LEFT JOIN
Why do not you try left join
SELECT *
FROM `ultrait_wpl_properties`
LEFT JOIN ultrait_wpl_property_types
ON ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
ORDER BY ultrait_wpl_properties.id
Related
I have a working query that fetches some results.
$sql = "
SELECT
swdl.wedding_dress AS wedding_dress,
wd.name AS name,
wdi.url AS image
FROM salons_wedding_dresses_link AS swdl
LEFT JOIN wedding_dresses AS wd ON swdl.wedding_dress = wd.id
LEFT JOIN wedding_dress_images AS wdi ON wdi.wedding_dress = wd.id AND wdi.main_image = 1
WHERE swdl.salon = ? AND wd.active = 1
";
What i want is to update it to only fetch results where wedding_dress in table salons_wedding_dresses_link is not located in table wedding_dress_dress_collection_link in column wedding_dress_id but i fail to do so.
Bellow is the updated query, with update in bold.
$sql = "
SELECT
swdl.wedding_dress AS wedding_dress,
wd.name AS name,
wdi.url AS image
FROM salons_wedding_dresses_link AS swdl
LEFT JOIN wedding_dress_dress_collection_link AS wddcl
------------------------------------------------------
LEFT JOIN wedding_dresses AS wd ON swdl.wedding_dress = wd.id
LEFT JOIN wedding_dress_images AS wdi ON wdi.wedding_dress = wd.id AND wdi.main_image = 1
WHERE
swdl.wedding_dress NOT IN (SELECT wddcl.wedding_dress_id FROM wddcl) AND
------------------------------------------------------------------------
swdl.salon = ? AND wd.active = 1
";
select query to get the result which is present in salons_wedding_dresses_link but not in wedding_dress_dress_collection_link:
select s1.*,s2.wedding_dress_collection_id,s2.wedding_dress_id
from salons_wedding_dresses_link s1
left join wedding_dress_dress_collection_link s2 on
s1.wedding_dress=s2.wedding_dress_id
whrere s2.wedding_dress_id is null;
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
The following query in phpmyadmin returns three columns (entry_id) with three different entry ids as I need.
SELECT sub_1.entry_id, sub_2.entry_id, sub_3.entry_id
FROM exp_judging_portfolios AS jud
LEFT JOIN exp_submissions AS sub_1 ON sub_1.id = jud.rel_id_1
LEFT JOIN exp_submissions AS sub_2 ON sub_2.id = jud.rel_id_2
LEFT JOIN exp_submissions AS sub_3 ON sub_3.id = jud.rel_id_3
WHERE sub_1.member_group = $member_group
AND jud.pre = 1
GROUP BY jud.rel_id_1
However, when I return the results in page, I get a single array with just one of the entry ids.
Here is the code I am using to generate the results
$sql = "
SELECT sub_1.entry_id, sub_2.entry_id, sub_3.entry_id
FROM exp_judging_portfolios AS jud
LEFT JOIN exp_submissions AS sub_1 ON sub_1.id = jud.rel_id_1
LEFT JOIN exp_submissions AS sub_2 ON sub_2.id = jud.rel_id_2
LEFT JOIN exp_submissions AS sub_3 ON sub_3.id = jud.rel_id_3
WHERE sub_1.member_group = $member_group
AND jud.pre = 1
GROUP BY jud.rel_id_1
";
$query = $this->EE->db->query($sql);
$submissions_portfolio = $query->result_array();
print_r($submissions_portfolio);
Here is whats returned:
Array ( [0] => 354 )
Does anyone have any idea why? and if so, how to return all 3 entry ids?
The reason you only get one result this time, is because all 3 selected ids have the same alias entry_id. They end up overwriting each other.
You should try naming them differently:
SELECT sub_1.entry_id AS id_1, sub_2.entry_id AS id_2, sub_3.entry_id AS id_3
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 need a MySQL query (Standard SQL) to get article information from drupal 7 (title, image, body, publish date). Only last revision.
Sounds like you want an EntityFieldQuery:
$query = new EntityFieldQuery;
$results = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'article')
->execute();
if (isset($results['node'])) {
$nodes = node_load_multiple(array_keys($results['node']));
foreach ($nodes as $node) {
$created = $node->created;
$image_uri = $node->field_image[$node->language][0]['uri'];
// ...
}
}
Ok, I found a solution:
SELECT node.title, body.body_value, FROM_UNIXTIME(node.created) AS Created, file_managed.uri AS image
FROM node INNER JOIN
field_data_body AS body ON node.nid = body.entity_id INNER JOIN
file_usage ON file_usage.id = node.nid INNER JOIN
file_managed ON file_usage.fid = file_managed.fid
WHERE (node.type = 'article')