Anybody got an idea what is wrong with this one?
At first I had a query without subselect, but I have articles in a database and each article can have multiple prices and I just need the actual one in my total result-set.
The error is as said in title "#1054 - Unknown column 'a.ArtikelID' in 'on clause' " but the columns are all perfectly valid, before I split price calculation into a subselect all columns were perfectly fine.
SELECT `a`.`ArtikelNr`, `at`.`name`, `a`.`LagerPlatz`, `a`.`Bestand`, `a`.`Reserviert`, `a`.`Sperrlager`, (`a`.`Bestand` - `a`.`Reserviert` - `a`.`Sperrlager`) AS `GesamtBestand`, `m`.`Prozent` AS `MwSt%`, `preis`.`Netto` AS `Einzelpreis`, (`preis`.`Netto` * `a`.`Bestand`) AS `Gesamtpreis`
FROM `artikel` a,
(
SELECT `psub`.`Netto`
FROM `artikel` asub
LEFT JOIN `preis` psub ON `asub`.`ArtikelID` = `psub`.`ArtikelID`
WHERE `asub`.`Bestandfuehren` = '1' AND `psub`.`PreisArtID` = 2 AND (`psub`.`Bis` = '0001-01-01 00:00:00' OR `psub`.`Bis` >= NOW())
ORDER BY `psub`.`Von` DESC
LIMIT 1
) AS preis
LEFT JOIN `artikel_text` at ON `a`.`ArtikelID` = `at`.`ArtikelID`
LEFT JOIN `system3zentral`.`mehrwertsteuer` m ON `a`.`MehrwertsteuerID` = `m`.`MehrwertsteuerID`
WHERE `a`.`Bestandfuehren` = '1' AND `at`.`SpracheID` = 1 AND `preis`.`Netto` IS NOT NULL
GROUP BY `a`.`ArtikelNr`
ORDER BY CASE WHEN LENGTH(`a`.`Lagerplatz`) <= 2 THEN 1 ELSE 0 END, `a`.`Lagerplatz` ASC
Solved it myself, thanks #Strawberry...
Here the solution for anybody who has the same problem when building a query...
SELECT `a`.`ArtikelNr`, `at`.`name`, `a`.`LagerPlatz`, `a`.`Bestand`, `a`.`Reserviert`, `a`.`Sperrlager`, (`a`.`Bestand` - `a`.`Reserviert` - `a`.`Sperrlager`) AS `GesamtBestand`, `m`.`Prozent` AS `MwSt%`, `preis`.`Netto` AS `Einzelpreis`, (`preis`.`Netto` * ABS(`a`.`Bestand`)) AS `Gesamtwert`
FROM `artikel` a
LEFT JOIN
(
SELECT `suba`.`ArtikelID` AS artID, `subp`.`Netto`
FROM `artikel` suba
LEFT JOIN `preis` subp ON `suba`.`ArtikelID` = `subp`.`ArtikelID`
WHERE `suba`.`Bestandfuehren` = '1' AND `subp`.`PreisArtID` = 2 AND (`subp`.`Bis` = '0001-01-01 00:00:00' OR `subp`.`Bis` >= NOW())
ORDER BY `subp`.`Von` DESC
) preis ON `a`.`ArtikelID` = `preis`.`artID`
LEFT JOIN `artikel_text` at USING(ArtikelID)
LEFT JOIN `system3zentral`.`mehrwertsteuer` m USING(`MehrwertsteuerID`)
WHERE `a`.`Bestandfuehren` = '1' AND `at`.`SpracheID` = 1 AND `preis`.`Netto` IS NOT NULL
GROUP BY `a`.`ArtikelNr`
ORDER BY CASE WHEN LENGTH(`a`.`Lagerplatz`) <= 2 THEN 1 ELSE 0 END, `a`.`Lagerplatz` ASC
Related
the following is the situation. I need to connect an order-table with a message-table. But i'm only interested in the first message(lowest message-id). The connection between the tables is the orderid.
$result = $this->db->executeS('
SELECT o.*, c.iso_code AS currency, s.name AS shippingMethod, m.message AS note
FROM '._DB_PREFIX_.'orders o
LEFT JOIN '._DB_PREFIX_.'currency c ON c.id_currency = o.id_currency
LEFT JOIN '._DB_PREFIX_.'message m ON m.id_order = o.id_order
LEFT JOIN '._DB_PREFIX_.'carrier s ON s.id_carrier = o.id_carrier
LEFT JOIN jtl_connector_link l ON o.id_order = l.endpointId AND l.type = 4
WHERE l.hostId IS NULL AND o.date_add BETWEEN DATE_SUB(NOW(), INTERVAL 1 WEEK) AND NOW()
GROUP BY o.id_order
HAVING MIN(m.id_message)
LIMIT '.$limit
);
This query works so far. But now orders without a message are missing.
Thank you for your help!
Markus
You want to select several orders and per order the first message. This is generally difficult in MySQL for the lack of window functions (e.g. ROW_NUMBER OVER). But as it's just one column from the message table you are interested in, you can use a subquery in the SELECT clause.
SELECT
o.*,
c.iso_code AS currency,
s.name AS shippingMethod,
(
SELECT m.message
FROM message m
WHERE m.id_order = o.id_order
ORDER BY m.id_message
LIMIT 1
) AS note
FROM orders o
JOIN currency c ON c.id_currency = o.id_currency
JOIN carrier s ON s.id_carrier = o.id_carrier
WHERE o.date_add BETWEEN DATE_SUB(NOW(), INTERVAL 1 WEEK) AND NOW()
AND NOT EXISTS
(
SELECT *
FROM jtl_connector_link l
WHERE l.endpointId = o.id_order
AND l.type = 4
);
query taking 1 minute to fetch results
SELECT
`jp`.`id`,
`jp`.`title` AS game_title,
`jp`.`game_type`,
`jp`.`state_abb` AS game_state,
`jp`.`location` AS game_city,
`jp`.`zipcode` AS game_zipcode,
`jp`.`modified_on`,
`jp`.`posted_on`,
`jp`.`game_referal_amount`,
`jp`.`games_referal_amount_type`,
`jp`.`status`,
`jp`.`is_flaged`,
`u`.`id` AS employer_id,
`u`.`email` AS employer_email,
`u`.`name` AS employer_name,
`jf`.`name` AS game_function,
`jp`.`game_freeze_status`,
`jp`.`game_statistics`,
`jp`.`ats_value`,
`jp`.`integration_id`,
`u`.`account_manager_id`,
`jp`.`model_game`,
`jp`.`group_id`,
(CASE
WHEN jp.group_id != '0' THEN gm.group_name
ELSE 'NA'
END) AS group_name,
`jp`.`priority_game`,
(CASE
WHEN jp.country != 'US' THEN jp.country_name
ELSE ''
END) AS game_country,
IFNULL((CASE
WHEN
`jp`.`account_manager_id` IS NULL
OR `jp`.`account_manager_id` = 0
THEN
(SELECT
(CASE
WHEN
account_manager_id IS NULL
OR account_manager_id = 0
THEN
`u`.`account_manager_id`
ELSE account_manager_id
END) AS account_manager_id
FROM
user_user
WHERE
id = (SELECT
user_id
FROM
game_user_assigned
WHERE
game_id = `jp`.`id`
LIMIT 1))
ELSE `jp`.`account_manager_id`
END),
`u`.`account_manager_id`) AS acc,
(SELECT
COUNT(recach_limit_id)
FROM
recach_limit
WHERE
recach_limit = '1'
AND recach_limit_game_id = rpr.recach_limit_game_id) AS somewhatgame,
(SELECT
COUNT(recach_limit_id)
FROM
recach_limit
WHERE
recach_limit = '2'
AND recach_limit_game_id = rpr.recach_limit_game_id) AS verygamecommitted,
(SELECT
COUNT(recach_limit_id)
FROM
recach_limit
WHERE
recach_limit = '3'
AND recach_limit_game_id = rpr.recach_limit_game_id) AS notgame,
(SELECT
COUNT(joa.id) AS applicationcount
FROM
game_refer_to_member jrmm
INNER JOIN
game_refer jrr ON jrr.id = jrmm.rid
INNER JOIN
game_applied joa ON jrmm.id = joa.referred_by
WHERE
jrmm.STATUS = '1'
AND jrr.referby_user_id IN (SELECT
ab_testing_user_id
FROM
ab_testing)
AND joa.game_post_id = rpr.recach_limit_game_id
AND (rpr.recach_limit = 1
OR rpr.recach_limit = 2)) AS gamecount
FROM
(`game_post` AS jp)
JOIN
`user_info` AS u ON `jp`.`user_user_id` = `u`.`id`
JOIN
`game_functional` jf ON `jp`.`game_functional_id` = `jf`.`id`
LEFT JOIN
`group_musesm` gm ON `gm`.`group_id` = `jp`.`group_id`
LEFT JOIN
`recach_limit` rpr ON `jp`.`id` = `rpr`.`recach_limit_game_id`
WHERE
`jp`.`status` != '3'
GROUP BY `jp`.`id`
ORDER BY `posted_on` DESC
LIMIT 10
I would first suggest not nesting select statements because this will cause an n^x performance hit on every xth level and I see at least 3 levels of selects inside this query.
Add index
INDEX(status, posted_on)
Move LIMIT inside
Then, instead of saying
FROM (`game_post` AS jp)
say
FROM ( SELECT id FROM game_post
WHERE status != 3
ORDER BY posted_on DESC
LIMIT 10 ) AS ids
JOIN game_post AS jp USING(id)
(I am assuming that the PK of jp is (id)?)
That should efficiently use the new index to get the 10 ids needed. Then it will reach back into game_post to get the other columns.
LEFT
Also, don't say LEFT unless you need it. It costs something to generate NULLs that you may not be needing.
Is GROUP BY necessary?
If you remove the GROUP BY, does it show dup ids? The above changes may have eliminated the need.
IN(SELECT) may optimize poorly
Change
AND jrr.referby_user_id IN ( SELECT ab_testing_user_id
FROM ab_testing )
to
AND EXISTS ( SELECT * FROM ab_testing
WHERE ab_testing_user_id = jrr.referby_user_id )
(This change may or may not help, depending on the version you are running.)
More
Please provide EXPLAIN SELECT if you need further assistance.
This query is showing error :
#1054 - Unknown column 'sp.spot_id' in 'on clause'
Query :
SELECT product.*,sp.sp_name FROM `product`
left join spot_selling on product.product_id=spot_selling.product_id
AND spot_selling.end_time >= now()
AND spot_selling.start_time <= now()
AND spot_selling.status='1'
left join(select GROUP_CONCAT(s.name SEPARATOR ',') as sp_name
from spot s group by s.spot_id) sp on sp.spot_id=spot_selling.spot_id
WHERE product.user_id='26' AND product.status!='6'
I think you just need spot_id in the subquery, so there is something to join on:
SELECT product.*, sp.sp_name
FROM `product` left join
spot_selling
on product.product_id=spot_selling.product_id AND
spot_selling.end_time >= now() AND
spot_selling.start_time <= now() AND
spot_selling.status = '1' left join
(select spot_id, GROUP_CONCAT(s.name SEPARATOR ',') as sp_name
--------------^
from spot s
group by s.spot_id
) sp
on sp.spot_id = spot_selling.spot_id
WHERE product.user_id = '26' AND product.status <> '6'
In relation to the answer I accepted for this post, SQL Group By and Limit issue, I need to figure out how to create that query using SQLAlchemy. For reference, the query I need to run is:
SELECT t.id, t.creation_time, c.id, c.creation_time
FROM (SELECT id, creation_time
FROM thread
ORDER BY creation_time DESC
LIMIT 5
) t
LEFT OUTER JOIN comment c ON c.thread_id = t.id
WHERE 3 >= (SELECT COUNT(1)
FROM comment c2
WHERE c.thread_id = c2.thread_id
AND c.creation_time <= c2.creation_time
)
I have the first half of the query, but I am struggling with the syntax for the WHERE clause and how to combine it with the JOIN. Any one have any suggestions?
Thanks!
EDIT: First attempt seems to mess up around the .filter() call:
c = aliased(Comment)
c2 = aliased(Comment)
subq = db.session.query(Thread.id).filter_by(topic_id=122098).order_by(Thread.creation_time.desc()).limit(2).offset(2).subquery('t')
subq2 = db.session.query(func.count(1).label("count")).filter(c.id==c2.id).subquery('z')
q = db.session.query(subq.c.id, c.id).outerjoin(c, c.thread_id==subq.c.id).filter(3 >= subq2.c.count)
this generates the following SQL:
SELECT t.id AS t_id, comment_1.id AS comment_1_id
FROM (SELECT count(1) AS count
FROM comment AS comment_1, comment AS comment_2
WHERE comment_1.id = comment_2.id) AS z, (SELECT thread.id AS id
FROM thread
WHERE thread.topic_id = :topic_id ORDER BY thread.creation_time DESC
LIMIT 2 OFFSET 2) AS t LEFT OUTER JOIN comment AS comment_1 ON comment_1.thread_id = t.id
WHERE z.count <= 3
Notice the sub-query ordering is incorrect, and subq2 somehow is selecting from comment twice. Manually fixing that gives the right results, I am just unsure of how to get SQLAlchemy to get it right.
Try this:
c = db.aliased(Comment, name='c')
c2 = db.aliased(Comment, name='c2')
sq = (db.session
.query(Thread.id, Thread.creation_time)
.order_by(Thread.creation_time.desc())
.limit(5)
).subquery(name='t')
sq2 = (
db.session.query(db.func.count(1))
.select_from(c2)
.filter(c.thread_id == c2.thread_id)
.filter(c.creation_time <= c2.creation_time)
.correlate(c)
.as_scalar()
)
q = (db.session
.query(
sq.c.id, sq.c.creation_time,
c.id, c.creation_time,
)
.outerjoin(c, c.thread_id == sq.c.id)
.filter(3 >= sq2)
)
I'm trying to run a select query, which is the following:
SELECT * FROM pages, c_item_category cc
LEFT JOIN pages_tr ON (pages.page_id = pages_tr.page_id AND lang_id = 2)
LEFT JOIN users ON (pages.page_author = users.u_id)
WHERE (pages.page_id = cc.item_id AND cc.cat_id = 7)
AND (page_date >= 1317420000 AND page_date <= 1320101999)
AND (page_showinfos = 1)
ORDER BY page_date ASC LIMIT 0,10
But I get this error: Unknown column 'pages.page_id' in 'on clause'
Why is that? I can confirm that I have a column named 'page_id' in 'pages' :)
But, when I do a single-table query with the same left joins, it works well:
SELECT * FROM pages
LEFT JOIN pages_tr ON (pages.page_id = pages_tr.page_id AND lang_id = 2)
LEFT JOIN users ON (pages.page_author = users.u_id)
WHERE (page_date >= 1317420000 AND page_date <= 1320101999)
AND (page_showinfos = 1)
ORDER BY page_date ASC LIMIT 0,10
What is my mistake in the first query? :\
Try it this way instead.
SELECT *
FROM pages
LEFT JOIN pages_tr ON (pages.page_id = pages_tr.page_id AND lang_id = 2)
LEFT JOIN users ON (pages.page_author = users.u_id),
c_item_category cc
WHERE (pages.page_id = cc.item_id AND cc.cat_id = 7)
AND (page_date >= 1317420000 AND page_date <= 1320101999)
AND (page_showinfos = 1)
ORDER BY page_date ASC LIMIT 0,10