I am trying to SELECT data from my database with the method LEFT OUTER JOIN but when I run my syntax the result is just one row. I am sure the result must be more rows.
I also tried the method FULL OUTER JOIN instead of LEFT OUTER JOIN. But when I do that I get an syntax error.
Does someone know why I am gettin just one row?
Here is my sql syntax:
SELECT
cus.cus_id
, cus.name_cus
, cus.address, count(invoice.id) as id2
, CONCAT('€ ', ROUND(SUM(invoice.total),2)) as total
, cus.id
FROM cus
LEFT OUTER JOIN invoice
ON cus.cus_id = invoice.cus_id
You are using aggregate functions without GROUP BY clause:
SELECT
cus.cus_id
, cus.name_cus
, cus.address
, count(invoice.id) as id2
, CONCAT('€ ', ROUND(SUM(invoice.total),2)) as total
, cus.id
FROM cus
LEFT OUTER JOIN invoice ON cus.cus_id = invoice.cus_id
GROUP BY cus.cus_id, cus.name_cus, cus.address, cus.id
Although you need to group only by the unique ID (i.e. cus.id) you should add other fields that are not aggregated to GROUP BY clause as well, even though they do not create additional groups.
Related
i want to solve these two problems with equi join
For each customer order, list the order id, order date, order_source_id, source description, and the first and last name of the customer.
For each line in shipment_line, display the shipment_id, inv_id, ship_quantity, date_expected and date_received.
This is my syntax but it is showing error.
1.
SELECT order_id,order_date,order_source_id,source_desc,first,last
FROM customer,cust_order,order_source
WHERE (cust_order.order_source_id = order_source.order_source_id)
AND cust_order.cust_id = customer.cust_id;
2.
SELECT shipment_line.shipment_id,shipment_line.inv_id,shipment_line.date_recieved,shipment.date_expected
FROM shipment_line, shipment
WHERE shipment_line.shipment_id = shipment.shipment_id;
You could have ambiguous columns so you should add the related table name for each column:
SELECT cust_order.order_id
, cust_order.order_date
, cust_order.order_source_id
, order_source.source_desc
, customer.first
, customer.last
FROM customer
INNER JOIN cust_order ON cust_order.cust_id = customer.cust_id
INNER JOIN order_source ON cust_order.order_source_id = order_source.order_source_id
And you should not use old (pre 1992) implicit join syntax based on where clause but use explicit join class.
(Same problem for the second query).
I have two tables: terems and logs1015.
Need to add data from logs1015 to terems based on similar 'hash' row.
This query works fine if 'SUM(insum)' or 'SUM(outsum) is larger than 0.
But if logs1015 doesn't contain data with such 'hash' then query result is empty.
What the mistake? Thanks
SELECT terems.*,
SUM(insum) as firstsum ,
SUM(outsum) as secondsum
FROM terems
LEFT JOIN logs1015 USING(hash)
WHERE owner='1'
AND (type='stat')
AND (time BETWEEN 1445904000 AND 1445990400)
GROUP BY name
LIMIT 1000
Tables structure
*terems*: id(int),hash(varchar),name(varchar),owner(int)
*logs1015*: id(int),hash(varchar),type(varchar),insum(int),outsum(int),time(varchar)
When (left) outer joining, you must put the where clauses on the outer table in the join condition, otherwise you say that it must exist after joining. And then you have implicitly turned it into an inner join.
Also use aliases on your tables so you can easily spot these bugs.
Example:
SELECT t.name,
SUM(l.insum) as firstsum ,
SUM(l.outsum) as secondsum
FROM terems t
LEFT JOIN logs1015 l ON t.hash = l.hash
AND (l.type='stat')
WHERE t.owner='1'
AND (t.time BETWEEN 1445904000 AND 1445990400)
GROUP BY t.name
LIMIT 1000
Working solution:
"SELECT t.*,
SUM(l.insum) as firstsum ,
SUM(l.outsum) as secondsum
FROM terems t
LEFT JOIN logs1015 l ON
t.hash = l.hash
AND (l.type='stat')
AND (l.time BETWEEN $fromstamp AND $tostamp)
WHERE t.owner='$userid'
GROUP BY t.name
LIMIT 1000";
Thanks a lot!
I am trying to LEFT JOIN this table field, but it gives me the title error.
The problem starts when I add the left join query line. I guess it should be somewhere else.
Here's the query:
SELECT questions.* , user_profile.first_name , user_profile.uIMG , user_profile.mid_name
,user_profile.last_name , q_categories.cat_title , qreports.u_ID
FROM questions , user_profile , q_categories
LEFT JOIN qreports ON questions.qID = qreports.q_ID
WHERE questions.uID=user_profile.UID AND questions.qID= 8
AND questions.cat_ID=q_categories.cat_ID
LIMIT 1
Your formatting and style is old and also not a proper joining. try this query and adjust the query (because the given query is not complete)
SELECT
q.*,u.first_name , u.uIMG ,u.mid_name ,u.last_name,qc.cat_title,qr.u_ID
FROM questions AS q
LEFT JOIN user_profile AS u ON q.uID=u.UID
LEFT JOIN q_categories AS qc ON q.catID=qc.catID
LEFT JOIN qreports AS qr ON q.qID=qr.qID
WHERE 1
In WHERE condition you can use any condition if you want
I will try to explain things as much as I can.
I have following query to fetch records from different tables.
SELECT
p.p_name,
p.id,
cat.cat_name,
p.property_type,
p.p_type,
p.address,
c.client_name,
p.price,
GROUP_CONCAT(pr.price) AS c_price,
pd.land_area,
pd.land_area_rp,
p.tagline,
p.map_location,
r.id,
p.status,
co.country_name,
p.`show`,
u.name,
p.created_date,
p.updated_dt,
o.type_id,
p.furnished,
p.expiry_date
FROM
property p
LEFT OUTER JOIN region AS r
ON p.district_id = r.id
LEFT OUTER JOIN country AS co
ON p.country_id = co.country_id
LEFT OUTER JOIN property_category AS cat
ON p.cat_id = cat.id
LEFT OUTER JOIN property_area_details AS pd
ON p.id = pd.property_id
LEFT OUTER JOIN sc_clients AS c
ON p.client_id = c.client_id
LEFT OUTER JOIN admin AS u
ON p.adminid = u.id
LEFT OUTER JOIN sc_property_orientation_type AS o
ON p.orientation_type = o.type_id
LEFT OUTER JOIN property_amenities_details AS pad
ON p.id = pad.property_id
LEFT OUTER JOIN sc_commercial_property_price AS pr
ON p.id = pr.property_id
WHERE p.id > 0
AND (
p.created_date > DATE_SUB(NOW(), INTERVAL 1 YEAR)
OR p.updated_dt > DATE_SUB(NOW(), INTERVAL 1 YEAR)
)
AND p.p_type = 'sale'
everything works fine if I exclude GROUP_CONCAT(pr.price) AS c_price, from above query. But when I include this it just gives one result. My intention to use group concat above is to fetch comma separated price from table sc_commercial_property_price that matches the property id in this case p.id. If the records for property exist in sc_commercial_property_price then fetch them in comma separated form along with other records. If not it should return blank. What m I doing wrong here?
I will try to explain again if my problem is not clear. Thanks in advance
The GROUP_CONCAT is an aggregation function. When you include it, you are telling SQL that there is an aggregation. Without a GROUP BY, only one row is returns, as in:
select count(*)
from table
The query that you have is acceptable syntax in MySQL but not in any other database. The query does not automatically group by the columns with no functions. Instead, it returns an arbitrary value. You could imagine a function ANY, so you query is:
select any(p.p_name) as p_num, any(p.tagline) as tagline, . . .
To fix this, put all your current variables in a group by clause:
GROUP BY
p.p_name,
p.id,
cat.cat_name,
p.property_type,
p.p_type,
p.address,
c.client_name,
p.price,
pd.land_area,
pd.land_area_rp,
p.tagline,
p.map_location,
r.id,
p.status,
co.country_name,
p.`show`,
u.name,
p.created_date,
p.updated_dt,
o.type_id,
p.furnished,
p.expiry_date
Most people who write SQL think it is good form to include all the group by variables in the group by clause, even though MySQL does not necessarily require this.
Add GROUP BY clause enumerating whatever you intend to have separate rows for. What happens now is that it picks some value for each result column and group_concats every pr.price.
I have this query in MySQL:
SELECT pr.*, pr7.value AS `room_price_high`
FROM `jos_hp_properties` pr
LEFT OUTER JOIN `jos_hp_properties2` pr7 ON pr7.property=pr.id
WHERE pr7.field=23
The jos_hp_properties table has 27 rows but the query only returns one. Based on this question I think it may be because of the WHERE clause. The jos_hp_properties2 table has fields id, property, field, value, where field is a foreign key to a third table (which I don't need to get data from).
Is there a way to select all the rows from the first table, including the value from table #2 where the field is 23 (or NULL if there is no field 23)?
Sure. Move the WHERE condition to the JOIN:
SELECT pr.*, pr7.value AS `room_price_high`
FROM `jos_hp_properties` pr
LEFT JOIN `jos_hp_properties2` pr7
ON pr7.property=pr.id
AND
pr7.field=23
You must place the pr7 criteria in the join, not in the where clause. The where clause works on the entire result set AFTER the join has been performed.
SELECT pr.*, pr7.value AS `room_price_high`
FROM `jos_hp_properties` pr
LEFT OUTER JOIN `jos_hp_properties2` pr7 ON pr7.property=pr.id and pr7.field=23
Try this:
SELECT pr.*, pr7.value AS `room_price_high`
FROM `jos_hp_properties` pr
LEFT OUTER JOIN `jos_hp_properties2` pr7 ON pr7.property=pr.id
WHERE (pr7.field=23 OR pr7.field is null)
You can also use a CTE (Common Table Expression) to do the select, then use the CTE to do the left join..
wrc (parentid, childid) as (
select parentid, childid
from placechild
where relationshipid in (select id from placerelationship where relationship = 'Winter Region Capital')
),
stw (cnid, coid, capid, st_or_te, sid, scid,wcid) as (
select s.cnid, s.coid, s.capid, s.st_or_te, s.sid, s.scid, w.childid
from stcap s
left join wrc w
on s.sid = w.parentid
)
select * from stw