How do I fix my code so it runs correctly? It keeps telling me I have Error Code:
Error Code: 1054. Unknown column 'Number_Of_Ticketed_Events' in 'order clause').
Below is my code
SELECT Venue.VenueID, Venue.Capacity, VenueName,
COUNT(*) AS Number_Of_Ticketed_EventsĀ
FROM (
(Events INNER JOIN Non_Ticketed_Events)
INNER JOIN Venue ON Venue.VenueID = Events.VenueID
)
GROUP BY Events.VenueID
ORDER BY Number_Of_Ticketed_Events DESC;
Each JOIN needs telling which columns to use to make the join between the 2 tables
SELECT Venue.VenueID, Venue.Capacity, VenueName,
COUNT(*) AS Number_Of_Ticketed_Events
FROM Events e
INNER JOIN Non_Ticketed_Events nt ON Events.someColumn = nt.someColumn
INNER JOIN Venue ON Venue.VenueID = Events.VenueID
. . .
Related
I have an error in purchasedetail table when I am trying to fetch this record. This is my select query:
SELECT itemstock.itemId
FROM itemstock
JOIN item ON item.itemId = purchasedetail.itemId
JOIN purchasemaster ON purchasemaster.purchaseMasterId = purchasedetail.purchaseMasterId
JOIN purchasedetail ON purchasedetail.itemId = item.itemId
JOIN party ON party.partyId = purchasemaster.partyId
WHERE
purchasemaster.partyId = ".$_REQUEST['partyId']."
AND itemstock.quantity > 0
GROUP BY itemName,itemCode
This is the error:
#1054 - Unknown column 'purchasedetail.itemId' in 'on clause'
When joining tables, the columns you join "on" have to belong to those tables. You are trying to join on columns from other tables that you haven't mentioned yet. Try this:
SELECT itemstock.itemId
FROM itemstock
JOIN item ON item.itemId = itemstock.itemId
JOIN purchasedetail ON purchasedetail.itemId = item.itemId
JOIN purchasemaster ON purchasemaster.purchaseMasterId = purchasedetail.purchaseMasterId
JOIN party ON party.partyId = purchasemaster.partyId
WHERE purchasemaster.partyId = 5 AND itemstock.quantity > 0 GROUP BY itemName,itemCode
I am getting a SQL error that I don't understand. This is the error I get:
Database error: Invalid SQL:
SELECT distinct prodotti.nome, prodotti.id, prodotti.image_news,
imgprod.path, imgprod.alt
FROM `prodotti`, `categorie`, `prodcat`,
`gruppi` INNER JOIN imgprod ON prodotti.id = imgprod.idprod
LEFT OUTER JOIN radiation
ON prodotti.radiation_id = radiation.id_radiation
LEFT OUTER JOIN installation
ON prodotti.installation_id = installation.id_installation
WHERE 1=1 and prodotti.id = prodcat.idprod
and prodcat.idcat = categorie.id
and categorie.idgruppo = gruppi.id
and (gruppi.nome like 'ANT%' OR gruppi.nome like 'WIR%')
AND radiosystem LIKE '%VHF%'
GROUP BY prodotti.id ORDER BY prodotti.nome ASC
And this is another error I'm getting:
INNER JOIN MySQL Error: 1054 (Unknown column 'prodotti.id' in 'on clause')
This works on an old server, but on a new server with php 5.3.16 I am getting these errors. Can you please explain what the error means?
Your query rewritten using only explicit ANSI-style joins might look like
SELECT p.id, p.nome, p.image_news, i.path, i.alt
FROM prodotti p JOIN prodcat pc
ON p.id = pc.idprod JOIN categorie c
ON pc.idcat = c.id JOIN gruppi g
ON c.idgruppo = g.id JOIN imgprod i
ON p.id = i.idprod LEFT JOIN radiation r
ON p.radiation_id = r.id_radiation LEFT JOIN installation n
ON p.installation_id = n.id_installation
WHERE 1 = 1
AND (g.nome LIKE 'ANT%' OR g.nome LIKE 'WIR%')
AND radiosystem LIKE '%VHF%'
GROUP BY p.id, p.nome, p.image_news, i.path, i.alt
ORDER BY p.nome
It's impossible to tell more than that not seeing exact table schemas.
I am getting error #1054 - Unknown column 'e.ProjectID' in 'on clause' with the following SQL
SELECT ProjectID
FROM Project as p
INNER JOIN (SELECT EquipID , EquipName , EquipTypeID , (EquipPrice*Quantity) As CstAmount FROM Equipment) as e
ON p.ProjectID=e.ProjectID
Your subquery doesn't have that column exposed to the outer query.
SELECT ProjectID
FROM Project As p
INNER
JOIN (
SELECT ProjectID
, EquipID
, EquipName
, EquipTypeID
, (EquipPrice*Quantity) As CstAmount
FROM Equipment
) As e
ON p.ProjectID = e.ProjectID
However, if you're only wanting to return the ProjectID then I suggest you perform this query instead:
SELECT ProjectID
FROM Project
WHERE EXISTS (
SELECT *
FROM Equipment
WHERE ProjectID = Project.ProjectID
)
You dont need any subquery. Just join project table with equipment table as following
SELECT p.ProjectID,e.EquipID ,e.EquipName ,e.EquipTypeID ,e.EquipPrice* e.Quantity as CstAmount from Project P join Equipment e on p.ProjectID=e.ProjectID
you could do this without subquery
SELECT p.ProjectID ,e.EquipID , e.EquipName , e.EquipTypeID , (e.EquipPrice * e.Quantity) As CstAmount
FROM Project as p
INNER JOIN Equipment as e
ON p.ProjectID= e.ProjectID
what are you trying to do? your query can be rewritten like the following but i doubt this is what you want
SELECT p.ProjectID
FROM Project as p
INNER JOIN Equipment as e
ON p.ProjectID = e.ProjectID
do you need to select your subquery's fields as well?
So the other day, I asked this question about how to combine three complex queries and found a way to do it. Now I'm trying to use those queries to update a field in the users table and can't find a way to make it work. Here's the query:
update users set field_sum =(
select sum(field_sum) from (
select sum(field_one) as field_sum
from t_a join t_b on (t_a.bid = t_b.id) where t_b.user_id=users.id
union all
select sum(field_two) as field_sum
from t_c join t_d on (t_c.did = t_d.id) where t_d.user_id=users.id
union all
select sum(field_three) as field_sum
from t_e where t_e.user_id=users.id
) as field_sumT
)
When I try to run it, I get the following error: ERROR 1054 (42S22): Unknown column 'users.id' in 'where clause'. When I try removing the .user_id=users.id bit from each where clause, it will run but ends up with the total sum of field_sum, not just the field_sum for that user. Is there any way to accomplish this?
Use:
UPDATE USERS u
LEFT JOIN (SELECT t_b.user_id,
SUM(field_one) as field_sum
FROM t_a
JOIN t_b on t_a.bid = t_b.id
GROUP BY t_b.user_id) a ON a.user_id = u.id
LEFT JOIN (SELECT t_d.user_id,
SUM(field_two) as field_sum
FROM t_c
JOIN t_d on t_c.did = t_d.id
GROUP BY t_d.user_id) b ON b.user_id = u.id
LEFT JOIN (SELECT t_e.user_id,
SUM(field_three) as field_sum
from t_e
GROUP BY t_e.user_id) c ON c.user_id = u.id
SET field_num = COALESCE(a.field_sum, 0) + COALESCE(b.field_sum, 0) + COALESCE(c.field_sum, 0)
Caveat
This will set any users with no records in the supporting rows to have a field_sum value of zero. Do you only want to update those with a record in at least one of those tables?
This is my current query:
$sel = "SELECT
db1t1.userid, db1t1.customer_id, db2t1.customers_id, db2t1.orders_id, db2t2.products_price
FROM
database1.table1 db1t1
LEFT JOIN database2.table1 db2t1 ON
db1t1.customer_id = db2t1.customers_id
LEFT JOIN database2.table2 db2t2 ON
db2t1.orders_id = db2t2.orders_id
WHERE db1t1.userid IN(
SELECT
l.userid, l.username, r.username, r.cus_id
FROM
database1.table3 l
LEFT JOIN database2.table4 r ON
l.username = r.username
WHERE r.cus_id = '1234'
)";
Error message:
Operand should contain 1 column(s)
The error occurred because that you returned a result with multiple columns to an IN clause.
Try this:
SELECT
`db1t1`.`userid`, `db1t1`.`customer_id`, `db2t1`.`customers_id`,
`db2t1`.`orders_id`, `db2t2`.`products_price`
FROM `database1`.`table1` AS `db1t1`
LEFT JOIN `database2`.`table1` AS `db2t1`
USING (`customers_id`)
LEFT JOIN `database2`.`table2` AS `db2t2`
USING (`orders_id`)
WHERE `db1t1`.`userid` IN (
SELECT `l`.`userid`
FROM `database1`.`table3` AS `l`
LEFT JOIN `database2`.`table4` AS `r`
USING (`username`)
WHERE `r`.`cus_id` = 1234
)
What are you trying to achieve ? Maybe we can find a better solution.
Also, I think that you should do an INNER JOIN instead of a LEFT JOIN in the subquery.