I have following sql statement:
INSERT INTO wk1_tbl (shohin_code, shohin_mei, variation_flag)
SELECT ha.HINCD, ha.HINNMA, if (g.goods_para_id IS NULL, 0, 1) AS variation
FROM ( SELECT KOSHINCD, count(HINCD) AS quatity
FROM sc.HINMTF
GROUP BY KOSHINCD ) AS group_set
INNER JOIN sc.HINMTA ha
ON ha.HINCD = group_set.KOSHINCD
INNER JOIN master_hankoya.goods g
ON ha.WEBHINID = g.goods_id
WHERE ha.HINKB = '2' and ha.DATKB <> '9';
I using INNER JOIN on multi-database ,so that you can see sc and master_hankoya is difference databases
When I run it ,I get error :
Unknown column 'g.goods_id ' in 'on clause'
You can see g alias for table master_hankoya.goods,and goods_id is a column in this
I guess that I have problem with INNER JOIN
Please help me correct it
Update : I check again and take a silly problem ,have a special character in query make it failed to run
try this
INSERT INTO wk1_tbl (shohin_code, shohin_mei, variation_flag)
SELECT ha.HINCD, ha.HINNMA, if (g.goods_para_id IS NULL, 0, 1) AS variation
FROM ( SELECT KOSHINCD, count(HINCD) AS quatity
FROM sc.HINMTF
GROUP BY KOSHINCD ) AS group_set
INNER JOIN sc.HINMTA ha
ON ha.HINCD = group_set.KOSHINCD
INNER JOIN
(select * from master_hankoya.goods ) g
ON ha.WEBHINID = g.goods_id
WHERE ha.HINKB = '2' and ha.DATKB <> '9';
Don't use alias name for master_hankoya.goods like
INSERT INTO wk1_tbl (shohin_code, shohin_mei, variation_flag)
SELECT ha.HINCD, ha.HINNMA, if (master_hankoya.goods.goods_para_id IS NULL, 0, 1) AS variation
FROM ( SELECT KOSHINCD, count(HINCD) AS quatity
FROM sc.HINMTF
GROUP BY KOSHINCD ) AS group_set
INNER JOIN sc.HINMTA ha
ON ha.HINCD = group_set.KOSHINCD
INNER JOIN master_hankoya.goods
ON ha.WEBHINID = master_hankoya.goods.goods_id
WHERE ha.HINKB = '2' and ha.DATKB <> '9';
INNER JOIN sc.HINMTA ha
Change this line to:
INNER JOIN HINMTA AS ha
INNER JOIN master_hankoya.goods g
Change that line to
INNER JOIN goods AS g
The syntax for setting an alias for a table is:
table_name AS table_alias
Related
This is my query
SELECT CONCAT(`SM_Title`,' ',`SM_Full_Name`) AS NAME,
`RG_Date`,
`RG_Reg_No`,
`RG_Stu_ID`,
`SM_Tell_Mobile`,
`SM_Tel_Residance`,
`RG_Reg_Type`,
Default_Batch,
`RG_Status`,
`RG_Final_Fee`,
`RG_Total_Paid`,
(`RG_Final_Fee`-`RG_Total_Paid`) AS TOTALDUE,
SUM(`SI_Ins_Amount` - `SI_Paid_Amount`) AS AS_AT_APRIAL_END
INNER JOIN
(SELECT `SI_Ins_Amount`,
`SI_Reg_No`
FROM
`student_installments`
GROUP BY MONTHNAME(`SI_Due_Date`)) Z ON
Z.`SI_Reg_No` = `registrations`.`RG_Reg_No`
FROM `registrations`
LEFT JOIN `student_master` ON `student_master`.`SM_ID` = `registrations`.`RG_Stu_ID`
LEFT JOIN `student_installments` ON `student_installments`.`SI_Reg_No` = `registrations`.`RG_Reg_No`
WHERE (`RG_Reg_Type` LIKE '%HND%' OR `RG_Reg_Type` LIKE '%LMU%' )
AND `SI_Due_Date` <= '2014-04-30' GROUP BY `SI_Reg_No`
It gave me an error near
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Z LIMIT 0, 25' at line 1
SELECT
CONCAT(SM_Title,' ',SM_Full_Name) AS NAME,
RG_Date,
RG_Reg_No,
RG_Stu_ID,
SM_Tell_Mobile,
SM_Tel_Residance,
RG_Reg_Type,
Default_Batch,
RG_Status,
RG_Final_Fee,
RG_Total_Paid,
(RG_Final_Fee-RG_Total_Paid) AS TOTALDUE,
SUM(SI_Ins_Amount - SI_Paid_Amount) AS AS_AT_APRIAL_END
FROM registrations
INNER JOIN
(SELECT SI_Ins_Amount,SI_Reg_No
FROM student_installments
GROUP BY MONTHNAME(SI_Due_Date)) Z ON Z.SI_Reg_No = registrations.RG_Reg_No
LEFT JOIN student_master ON student_master.SM_ID = registrations.RG_Stu_ID
LEFT JOIN student_installments ON student_installments.SI_Reg_No = registrations.RG_Reg_No
WHERE (RG_Reg_Type LIKE '%HND%' OR RG_Reg_Type LIKE '%LMU%' )
AND SI_Due_Date <= '2014-04-30'
GROUP BY SI_Reg_No
I notice you have fogotten the left table or subselect that you want to join to the (SELECT SI_INs .....) and previous this I could see there is no from clause before join.
I hope this helps you
Regards
You are using from clause in wrong position it should be just after selection of your columns, you can use below query:
SELECT
CONCAT(SM_Title,' ',SM_Full_Name) AS NAME ,RG_Date,RG_Reg_No,RG_Stu_ID,SM_Tell_Mobile,SM_Tel_Residance,RG_Reg_Type,Default_Batch,RG_Status,RG_Final_Fee,RG_Total_Paid,(RG_Final_Fee-RG_Total_Paid) AS TOTALDUE, SUM(SI_Ins_Amount - SI_Paid_Amount) AS AS_AT_APRIAL_END
FROM registrations AS reg
JOIN
(SELECT
SI_Ins_Amount,SI_Reg_No
FROM student_installments
GROUP BY MONTHNAME(SI_Due_Date)) AS Z
ON Z.SI_Reg_No = reg.RG_Reg_No
LEFT JOIN student_master AS sm
ON sm.SM_ID = reg.RG_Stu_ID
LEFT JOIN student_installments AS si
ON si.SI_Reg_No = reg.RG_Reg_No
WHERE (RG_Reg_Type LIKE '%HND%' OR RG_Reg_Type LIKE '%LMU%' ) AND SI_Due_Date <= '2014-04-30'
GROUP BY SI_Reg_No;
In the part below, from keyword should go before the inner join:
FROM registrations
INNER JOIN
(SELECT SI_Ins_Amount,
SI_Reg_No
FROM student_installments
GROUP BY MONTHNAME(SI_Due_Date)
) Z
ON Z.SI_Reg_No = registrations.RG_Reg_No
I have a problem that I need a WHERE clause in a subquery that depends on the results of the main Query, otherwise my results would be wrong and the query takes too long / is not executeable.
The circumstances that I need this query to create a view which I need for a search server support the problem that I cannot split this into two queries, nor process it with a script dynamically.
The problem occurs with the following query:
SELECT `s`.`id` AS `seminar_id`, (SUM( `sub`.`seminar_rate` ) / COUNT( `sub`.`seminar_id` )) AS `total_rate`
FROM
(
SELECT (SUM( value ) / COUNT( * )) AS `seminar_rate` , `r`.`seminar_id`
FROM `rating` r
INNER JOIN `rating_item` ri ON `r`.`id` = `ri`.`rating_id`
WHERE `r`.`seminar_id` = `s`.`id`/* <- Here is my problem, this is inacessible */
GROUP BY `r`.`seminar_id`
) AS sub,
`seminar` s
INNER JOIN `date` d
ON `s`.`id` = `d`.`seminar_id`
INNER JOIN `date_unit` du
ON `d`.`id` = `du`.`date_id`
LEFT JOIN `seminar_subject` su
ON `s`.`id` = `su`.`seminar_id`
LEFT JOIN `subject` suj
ON `su`.`subject_id` = `suj`.`id`
INNER JOIN `user` u
ON `s`.`user_id` = `u`.`id`
INNER JOIN `company` c
ON `u`.`company_id` = `c`.`id`
GROUP BY `du`.`date_id`, `sub`.`seminar_id`
This query should calculate a total rate out of ratings for each Seminar.
However my ratings are stored in my "rating" table and should be processed live.
(Sidenote: If you wonder about all the joins: This query has alooot more SELECT'ed fields, I just removed them because they are not nesessary to solve the problem and to make the query look less complicated [I know it still is >.>]...)
The reason is that I want this results to be sortable by my search engine later depending
on the users sort parameters, thatswhy I need it inside this query.
The problem itself is pretty obvious:
ERROR 1054 (42S22): Unknown column 's.id' in 'where clause'
The subselect doesnt know about the results of the main query, is there a solution to bypass this?
Could someone give me a hint to get this working?
Thanks in advance.
Using your subquery in the JOIN you can eliminate the WHERE clause and achieve nearly the same result. Here is your modified query. Hope this solves your problem.
SELECT `s`.`id` AS `seminar_id`, (SUM( `sub`.`seminar_rate` ) / COUNT( `sub`.`seminar_id` )) AS `total_rate`
FROM `seminar` s
INNER JOIN
(
SELECT (SUM( value ) / COUNT( * )) AS `seminar_rate` , `r`.`seminar_id`
FROM `rating` r
INNER JOIN `rating_item` ri ON `r`.`id` = `ri`.`rating_id`
/*WHERE `r`.`seminar_id` = `s`.`id` <- Here is my problem, this is inacessible */
GROUP BY `r`.`seminar_id`
) AS sub ON s.id = sub.`seminar_id`
INNER JOIN `date` d
ON `s`.`id` = `d`.`seminar_id`
INNER JOIN `date_unit` du
ON `d`.`id` = `du`.`date_id`
LEFT JOIN `seminar_subject` su
ON `s`.`id` = `su`.`seminar_id`
LEFT JOIN `subject` suj
ON `su`.`subject_id` = `suj`.`id`
INNER JOIN `user` u
ON `s`.`user_id` = `u`.`id`
INNER JOIN `company` c
ON `u`.`company_id` = `c`.`id`
GROUP BY `du`.`date_id`, `sub`.`seminar_id`
SELECT IFNULL(oea.`name`,op.name) AS "Consultation"
FROM `a` ca
JOIN `b` oea ON ca.`consultation_id` = oea.id AND oea.status = 1
LEFT OUTER JOIN c oeal ON oea.`location_id` = oeal.id
WHERE oea.employee_profile_id IN (
SELECT profile_id
FROM d pp
WHERE pp.status='1'
UNION
SELECT op.id
FROM e op
WHERE op.status
) AND
ca.status = 1
I am getting this error
Unknown column 'op.name' in 'field list'
Is there any possibility to use op.name outside the scope?
JOIN the subquery in the WHERE clause instead of using the IN predicate. This way you can select the name from it.
But you didn't select the column name from in the subquery. You selected only the profile_id, you have to select it with the profile_id. Something like:
SELECT
IFNULL(oea.`name`, op.name) AS Consultation
FROM `a` ca
JOIN `b` oea ON ca.`consultation_id` = oea.id AND oea.status = 1
INNER JOIN
(
SELECT profile_id, name
FROM d pp
WHERE pp.status='1'
UNION
SELECT op.id, name
FROM e op
WHERE op.status
) AS op ON oea.employee_profile_id = op.profile_id
LEFT OUTER JOIN c oeal ON oea.`location_id` = oeal.id
WHERE ca.status = 1;
I assumed that the two tables have the name column, if one of them didn't have it, you have to use null instead.
I a developing in zend and have a rather large mysql query. The query works fine and i get the list I expect. I am doing this using Select->Where.... below is the query.
SELECT DISTINCT `d`.* FROM `deliverable` AS `d` INNER JOIN `groups` AS `g1` ON d.id = g1.deliverable_id INNER JOIN `groupmembers` AS `gm1` ON g1.id = gm1.group_id LEFT JOIN `connection` AS `c` ON d.id = c.downstreamnode_id LEFT JOIN `deliverable` AS `d1` ON c.upstreamnode_id = d1.id INNER JOIN `deliverable` AS `d2` ON CASE WHEN d1.id IS NULL THEN d.id ELSE d1.id END = d2.id INNER JOIN `groups` AS `g` ON d2.id = g.deliverable_id INNER JOIN `groupmembers` AS `gm` ON g.id = gm.group_id WHERE (g1.group_type = 100) AND (gm1.member_id = 1) AND (c.downstreamnode_id IS NULL OR d.restrict_access = 1) AND (g.group_type = 100 OR g.group_type = 110) AND (gm.member_id = 1) AND (d.deliverable_type = 110 OR d.deliverable_type = 100) GROUP BY CASE WHEN c.downstreamnode_id IS NULL THEN d.id ELSE c.downstreamnode_id END
Only problem is when I try to count the rows in a mysql query I only get 1 returned. below is the query
SELECT DISTINCT count(*) AS `rowCount` FROM `deliverable` AS `d` INNER JOIN `groups` AS `g1` ON d.id = g1.deliverable_id INNER JOIN `groupmembers` AS `gm1` ON g1.id = gm1.group_id LEFT JOIN `connection` AS `c` ON d.id = c.downstreamnode_id LEFT JOIN `deliverable` AS `d1` ON c.upstreamnode_id = d1.id INNER JOIN `deliverable` AS `d2` ON CASE WHEN d1.id IS NULL THEN d.id ELSE d1.id END = d2.id INNER JOIN `groups` AS `g` ON d2.id = g.deliverable_id INNER JOIN `groupmembers` AS `gm` ON g.id = gm.group_id WHERE (g1.group_type = 100) AND (gm1.member_id = 1) AND (c.downstreamnode_id IS NULL OR d.restrict_access = 1) AND (g.group_type = 100 OR g.group_type = 110) AND (gm.member_id = 1) AND (d.deliverable_type = 110 OR d.deliverable_type = 100) GROUP BY CASE WHEN c.downstreamnode_id IS NULL THEN d.id ELSE c.downstreamnode_id END
i generate this from by using the same 'select' that generated the first query but I reset the columns and add count in.
$this->getAdapter()->setFetchMode(Zend_Db::FETCH_ASSOC);
$select
->reset( Zend_Db_Select::COLUMNS)
->columns(array('count('.$column.') as rowCount'));
$rowCount = $this->getAdapter()->fetchOne($select);
This method works fine for all my other queries only this one i am having trouble with. I suspect it has something to do the 'CASE' I have in there but it is strange because I am getting the correct rows the the first query. Any ideas. Thanks.
FYI below are two queries that I have working successfully.
SELECT DISTINCT `po`.* FROM `post` AS `po` INNER JOIN `postinfo` AS `p` ON po.postinfo_id = p.id WHERE (p.creator_id = 1) ORDER BY `p`.`date_created` DESC
SELECT DISTINCT count(*) AS `rowCount` FROM `post` AS `po` INNER JOIN `postinfo` AS `p` ON po.postinfo_id = p.id WHERE (p.creator_id = 1) ORDER BY `p`.`date_created` DESC
In this one I have 4 rows returned in the first query and 'int 4' returned for the second one. Does anyone know why it doesnt work for the big query?
Move your DISTINCT.
SELECT COUNT(DISTINCT `po`.*) AS `rowCount` ...
Ok figured it out It was the GROUP BY that was causing only 1 result to be returned. Thanks Interrobang for you help I am sure that using DISTINCT incorrectly will have caused me a headache in the future.
Try using SQL_CALC_FOUND_ROWS in your query?
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
Using SQL_CALC_FOUND_ROWS is mysql-specific, but it's pretty nice for getting a full record count even when your initial query contains a limit. Once you get the count, don't include SQL_CALC_FOUND_ROWS in subsequent queries for extra records since that will cause extra load on your query.
Your initial query would be:
SELECT SQL_CALC_FOUND_ROWS DISTINCT `d`.* FROM `deliverable` AS `d` INNER JOIN `groups` ...
You'll have to do a subsequent call after your initial query executes to get the count by doing a SELECT FOUND_ROWS().
If you do a little searching, you'll find someone who extended Zend_Db_Select to include this ability.
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.