join two inline tables - unknown column? - mysql

I'm getting "Error Code: 1054. Unknown column 'E1.extensao' in 'field list'". I changed table names but my query is as follows:
SELECT E1.stateName, E1.city, E1.boughtEnough, E2.bought
FROM
(SELECT count(DISTINCT idClient),city,stateName FROM
(SELECT
max(n.ordervalue) as boughtEnough,
m.idClient,
i.nome AS city,
i.id AS cityId,
i.stateName
FROM Clients c
INNER JOIN client_order m ON c.idClient = m.idClient
INNER JOIN orders n ON m.client_order = n.client_order
INNER JOIN orderDetail p ON n.idorder = p.idorder
AND p.idCurso = m.idCurso
INNER JOIN cities i ON c.city = i.id
WHERE
m.idCurso = '10'
GROUP BY
m.idClient,
i.nome,
i.id,
i.stateName
HAVING max(n.orders) >= 6) t
GROUP BY t.city, t.stateName
ORDER BY t.stateName,t.city) E1
JOIN (SELECT count(DISTINCT idClient),city,stateName FROM
(SELECT
count(n.ordervalue) as bought,
m.idClient,
i.nome AS city,
i.id AS cityId,
i.stateName
FROM Clients c
INNER JOIN client_order m ON c.idClient = m.idClient
INNER JOIN orders n ON m.client_order = n.client_order
INNER JOIN orderDetail p ON n.idorder = p.idorder
AND p.idCurso = m.idCurso
INNER JOIN cities i ON c.city = i.id
WHERE
m.idCurso = '10'
GROUP BY
m.idClient,
i.nome,
i.id,
i.stateName
HAVING ((max(n.orders) < 6) AND (count(n.orders) >= 1))) t
GROUP BY t.city, t.stateName
ORDER BY t.stateName,t.city) E2 ON E1.cityId = E2.cityId
I'm more used to SQL Server, not MySQL. What am I getting wrong?

I assume that E1.extensao means E1.boughtEnough? Look closely at how E1 is defined:
(SELECT count(DISTINCT idClient),city,stateName FROM
(SELECT
max(n.ordervalue) as boughtEnough,
...) t
...) E1
There's a t.boughtEnough, but you're not "passing it up" to E1.

Related

Get Value from INNER JOIN 3 table with LASTEST RECORD from each table

I get problem to get value from some of tables. You can see picture below, I wanna get row what I block with red color.
I try with code below
SELECT p.id,
p.email,
p.name,
p.lastname,
p.gender,
ex.startwork,
ex.endwork,
e.degree,
e.majority,
j.division
FROM job_jobseeker AS p
INNER JOIN job_experience AS ex
ON p.email = (SELECT ex.email
FROM job_experience
ORDER BY ex.id DESC
LIMIT 1)
INNER JOIN job_education AS e
ON p.email = (SELECT e.email
FROM job_education
ORDER BY ex.id DESC
LIMIT 1)
INNER JOIN job_applying AS j
ON p.email = (SELECT j.email
FROM job_applying
ORDER BY ex.id DESC
LIMIT 1)
You need correlated sub-queries.
Find the latest id for each email in all the three tables
SELECT startwork,
endwork,
email
FROM job_experience a
WHERE a.id = (SELECT Max(b.id)
FROM job_experience b
WHERE a.email = b.email)
The above query will find the latest id for each email in job_experience table. Do the same for other two tables as well, then join the result with job_jobseeker table to get the result.
SELECT p.id,
p.email,
p.name,
p.lastname,
p.gender,
ex.startwork,
ex.endwork,
e.degree,
e.majority,
j.division
FROM job_jobseeker AS p
INNER JOIN (SELECT startwork,
endwork,
email
FROM job_experience a
WHERE a.id = (SELECT Max(b.id) FROM job_experience b
WHERE a.email = b.email)) AS ex
ON p.email = ex.email
INNER JOIN (SELECT email, //Just called column without initialize
degree,
majority
FROM job_education a
WHERE a.id = (SELECT Max(b.id) FROM job_education b
WHERE a.email = b.email)) AS e
ON p.email = e.email
INNER JOIN (SELECT email, //Just called column without initialize
division
FROM job_applying a
WHERE a.id = (SELECT Max(b.id) FROM job_applying b
WHERE a.email = b.email)) AS j
ON p.email = j.email

SQL: join select multiple tables with missing row

I have a big multiple table query join select where some values are optional.
This is the query:
SELECT a.date_in, a.date_out, b.name, b.phone, b.birthdate,
b.country, b.hotel, b.room_nr, b.passport_nr, c.email,
d.size, e.name, GROUP_CONCAT(DISTINCT g.service), GROUP_CONCAT(DISTINCT h.service)
, GROUP_CONCAT(DISTINCT i.time), GROUP_CONCAT(DISTINCT j.location)
FROM reservation a, rider b, user c,
bike_size d, bike e, services_reservation f, services g,
bike_shipping h, bike_shipping_reservation i
, bike_shipping_location j WHERE a.rider_id = b.id
AND b.user_id = c.id AND a.bike_size_id = d.id AND
d.bike_id = e.id AND a.id = f.reservation_id AND
f.services_id = g.id
AND h.id = i.bike_shipping_id AND a.id = i.reservation_id
AND i.bike_shipping_location_id = j.id
AND a.id = 80
In the tables from the query above, the table named services_reservation with the following columns (id, services_id, reservation_id) is completely empty in this case, which makes the values that I select from the table bike_shipping_reservation NULL.
How can I make some tables that I select from optional in case they are empty?
Here is the SQL Fiddle with 1 empty table, you can see the NULL results at the end (only GROUP_CONCAT(DISTINCT g.service) should be NULL).
http://sqlfiddle.com/#!9/ee31b/6
Here is the SQL Fiddle with all tables having values in there columns, you can see that all values are returned not NULL.
http://sqlfiddle.com/#!9/8bc033/34
Any thoughts?
Use left join where the table (or the row) are empty on don't match
SELECT a.date_in, a.date_out, b.name, b.phone, b.birthdate,
b.country, b.hotel, b.room_nr, b.passport_nr, c.email,
d.size, e.name, GROUP_CONCAT(DISTINCT g.service), GROUP_CONCAT(DISTINCT h.service)
, GROUP_CONCAT(DISTINCT i.time), GROUP_CONCAT(DISTINCT j.location)
FROM reservation a
INNER JOIN rider b on a.rider_id = b.id
INNER JOIN user c on b.user_id = c.id
INNER JOIN bike_size d on a.bike_size_id = d.id
INNER JOIN bike e ON d.bike_id = e.id
LEFT JOIN services_reservation f on a.id = f.reservation_id
INNER JOIN services g on f.services_id = g.id
INNER JOIN bike_shipping_reservation i on a.id = i.reservation_id
INNER JOIN bike_shipping h ON h.id = i.bike_shipping_id
INNER JOIN bike_shipping_location j on i.bike_shipping_location_id = j.id
where a.id = 80

How to remove some data not needed on new row from mysql query with rollup

Hello I have a problem with the rollup mysql query. I want to get total of each column. I use mysql SUM() and ROLLUP to get the result. But on the new row that is added it copies the data of the last row. How can I remove those data. I provided an image on what I really needed to achieve.
Here is my query.
SELECT IFNULL(payments.id, "General Total"), payments.driver_id, payments.vehicle_specifications_id,payments.admins_id, payments.vehicle_id, payments.boundaries, payments.cashbond_payments, payments.loans, payments.penalties, SUM(payments.total_payments), a.first_name, a.mid_name, a.last_name, d2.date, d.first_name, d.mid_name, d.last_name, v.plate_number, v2.car_rate FROM payments INNER JOIN admins AS a ON payments.admins_id = a.id INNER JOIN drivers AS d ON payments.driver_id = d.id INNER JOIN dispatch AS d2 ON payments.dispatch_id =d2.id INNER JOIN vehicles AS v ON payments.vehicle_id = v.id INNER JOIN vehicle_specifications AS v2 ON payments.vehicle_specifications_id = v2.id GROUP BY payments.id WITH ROLLUP
Try this using union all
SELECT
payments.id,
payments.driver_id,
payments.vehicle_specifications_id,
payments.admins_id,
payments.vehicle_id,
payments.boundaries,
payments.cashbond_payments,
payments.loans,
payments.penalties,
SUM(payments.total_payments),
a.first_name,
a.mid_name,
a.last_name,
d2.date,
d.first_name,
d.mid_name,
d.last_name,
v.plate_number,
v2.car_rate
FROM payments
INNER JOIN admins AS a ON payments.admins_id = a.id
INNER JOIN drivers AS d ON payments.driver_id = d.id
INNER JOIN dispatch AS d2 ON payments.dispatch_id =d2.id
INNER JOIN vehicles AS v ON payments.vehicle_id = v.id
INNER JOIN vehicle_specifications AS v2 ON payments.vehicle_specifications_id = v2.id
GROUP BY payments.id
union all
select
'General Total' as id,
'' as driver_id,
'' as vehicle_specifications_id,
'' as admins_id,
'' as vehicle_id,
'' as boundaries,
'' as cashbond_payments,
'' as loans,
'' as penalties,
SUM(payments.total_payments),
'' as first_name,
'' as mid_name,
'' as last_name,
'' as date,
'' as first_name,
'' as mid_name,
'' as last_name,
'' as plate_number,
'' as car_rate
FROM payments
INNER JOIN admins AS a ON payments.admins_id = a.id
INNER JOIN drivers AS d ON payments.driver_id = d.id
INNER JOIN dispatch AS d2 ON payments.dispatch_id =d2.id
INNER JOIN vehicles AS v ON payments.vehicle_id = v.id
INNER JOIN vehicle_specifications AS v2 ON payments.vehicle_specifications_id = v2.id

inner join 4 tables with group, order by, having clause

I have 4 table and i want to extract: id, nume, localitate, masina_id, nr_inmatriculare, an_fabricatie, rafinarie, marca, and sum (quantity+deliver_quantity) as total_quantity group by an_fabricatie , Order by marca, and put some having clouse.
I don’t know how to make this.
My query is as bellow , but I think isn't correct.
select c.id, c.nume,c.localitate,l.masina_id, i.nr_inmatriculare, i.an_fabricatie,
i.rafinarie, m.marca from clienti c inner join livrari l on c.id = l.id inner join incarcari I on l.incarcare_id = l.livrari_id inner join masina m on i.id_marca = m.id, sum(select quantity, deliver_quantity) as total_quantity group by an_fabricatie having quantity >1000 order by marca;
Incarcari table
Id|livrari_id|id_marca|nr_inmatriculare|an_fabricatie|rafinarie|aviz_incarcare|quantity|
Livrari table
Id|masina_id|client_id|incarcare_id|deliver_quantity|aviz_livrare
Masini table
Id|numar_inmatriculare|marca|an_fabricatie|
Clienti table
Id|nume|localitate|date_add|date_upd|
SELECT c.id, c.nume, c.localitate, l.masina_id, i.nr_inmatriculare, i.an_fabricatie, i.rafinarie, m.marca, (SUM(i.quantity) + SUM(l.deliver_quantity)) AS total_quantity
FROM clienti c
INNER JOIN livrari l ON c.id = l.id
INNER JOIN incarcari i ON l.incarcare_id = i.livrari_id
INNER JOIN masini m ON i.id_marca = m.id
GROUP BY i.an_fabricatie, c.id, c.nume,c.localitate,l.masina_id, i.nr_inmatriculare, i.rafinarie, m.marca
HAVING i.quantity > 1000
ORDER BY m.marca DESC;

only returning records when s.id = u.summary_id

select
s.id, s.description, s.improvement, s.previous_year_id,
s.current_year_id, s.first_name, s.last_name, s.username,
s.finding, s.action, s.share, s.learned, s.timestamp,
d.title as department_title,
group_concat(g.title SEPARATOR \' | \') as strategic_goals,
y1.year as current_year_title, y2.year as previous_year_title,
u.summary_id, u.file_name as file_name
from
summary s, year y1, year y2, strategic_goal_entries sge,
goal g, department d, uploads u
where
s.id = sge.summary_id
and
s.current_year_id = y1.id
and
s.previous_year_id = y2.id
and
sge.goal_id = g.id
and
s.id = u.summary_id
and
s.department_id = d.id
and
s.department_id = '4'
group by
s.id
This only returns records from the summary table that has a relating record in the uploads table (s.id = uploads.summary_id) that contain a value within the uploads.summary_id field
I want to return all records, whether or not it has a file associated with it.
Any help is appreciated.
Suggest refactoring this SQL query to use ANSI joins. To achive your goal, you'd want a LEFT JOIN instead:
SELECT /*your columns*/
from summary s
INNER JOIN year y1 ON s.current_year_id = y1.id
INNER JOIN year y2 ON s.previous_year_id = y2.id
INNER JOIN strategic_goal_entries sge ON s.id = sge.summary_id
INNER JOIN goal g ON sge.goal_id = g.id
INNER JOIN department d ON s.department_id = d.id
LEFT JOIN uploads u ON s.id = u.summary_id
WHERE s.department_id = '4'
group by s.id