Mysql query, select 2 databases with 4 tables + nested SELECT? - mysql

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.

Related

Unknown field on INNER JOIN from multi databases

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

How to write multiple joins in MySQL

I am trying to a join a table with a second table created from another join. I'm not sure if i'm doing this correctly. I am receving an error code that "Every derived table must have its own alias". Creating an alias is not something I have done before. Could you please let me know how to adjust my query? Thank you.
Select
questionnaire_status.*
FROM
questionnaire_status
LEFT JOIN
(SELECT
rep_list.Last_Name,
rep_list.First_Name,
dept_codes.dept_name
FROM
rep_list
Left Join
dept_codes
ON
rep_list.dept =dept_codes.dept_id
)
ON
questionnaire_status.RR = rep_list.RR
WHERE
questionnaire_Status.Attestation_submitted = '0'
You are currently using subquerys, something you should not do unless absolutely necessary. (<- Performance is very bad)
Better are Multiple Joins are like this:
SELECT
questionnaire_status.*,
rep_list.Last_Name,
rep_list.First_Name,
dept_codes.dept_name
FROM
questionarrie_status
LEFT JOIN
rep_list
ON
questionnaire_status.RR = rep_list.RR
LEFT JOIN
dept_codes
ON
rep_list = dept_codes.dept_id
LEFT JOIN
questionarrie_status
WHERE
questionnaire_Status.Attestation_submitted = '0'
Use the as keyword to alias a subquery thus:
Select
questionnaire_status.*
FROM
questionnaire_status
LEFT JOIN
(SELECT
rep_list.Last_Name,
rep_list.First_Name,
rep_list.RR,
dept_codes.dept_name
FROM
rep_list
Left Join
dept_codes
ON
rep_list.dept =dept_codes.dept_id
) as tbl_alias
ON
questionnaire_status.RR = tbl_alias.RR
WHERE
questionnaire_Status.Attestation_submitted = '0
but you need to add the join column to the inner select as above
To get the results I think you're trying to get, all you have to do is this:
Select
questionnaire_status.*
FROM
questionnaire_status
LEFT JOIN rep_list ON questionnaire_status.RR = rep_list.RR
LEFT JOIN dept_codes ON rep_list.dept = dept_codes.dept_id
As for the aliases, when you do this you're creating the alias "me":
SELECT * FROM myTable me;
MySQL was complaining about your subquery not having an alias:
(SELECT
rep_list.Last_Name,
rep_list.First_Name,
dept_codes.dept_name
FROM
rep_list
Left Join
dept_codes
ON
rep_list.dept =dept_codes.dept_id
)
^^^ It wanted an alias here, to cover the subquery
So the next time you have a subquery, do something like this:
(SELECT
rep_list.Last_Name,
rep_list.First_Name,
dept_codes.dept_name
FROM
rep_list
Left Join
dept_codes
ON
rep_list.dept =dept_codes.dept_id
) REP
^^^ MySQL has an alias (named REP) so it's happy now

Can I convert this to a sub query driven query? As it stands it returns over a million rows due to LEFT JOIN

Here is the Query simplified:
SELECT
`Node`.`id`,
`Node`.`name`
FROM
`localhost`.`nodes` AS `Node`
LEFT JOIN `localhost`.`data_date_times` AS `DataDateTime` ON (
`DataDateTime`.`node_id` = `Node`.`id`
)
LEFT JOIN `localhost`.`data_locations` AS `DataLocation` ON (
`DataLocation`.`node_id` = `Node`.`id`
)
Are there any down sides to this VS a left join? Can I order the results for the overall query by returned values from the subqueries?
My stab at formatting this query:
SELECT
`Node`.`id`,
`Node`.`name`,
(SELECT `data_locations`.`name` FROM `data_locations` WHERE `data_locations`.`node_id` = `Node`.`id`),
(SELECT `data_date_times`.`name` FROM `data_date_times` WHERE `data_date_times`.`node_id` = `Node`.`id`)
FROM
`localhost`.`nodes` AS `Node`
WHERE ???
ORDER BY ???

Trying to add a subquery to a join query

I have a query like this
SELECT
tbl_products.*,
GROUP_CONCAT(tags.name)
FROM
tbl_page_collections_products,
(SELECT page_collection_name as name
FROM tbl_page_collections
LEFT JOIN tbl_pages ON tbl_page_collections.page_id = tbl_pages.page_id
WHERE tbl_pages.page_name LIKE '%friends%') tags
LEFT JOIN tbl_page_collections
ON tbl_page_collections.page_collection_id = tbl_page_collections_products.colID
LEFT JOIN tbl_pages
ON tbl_page_collections.page_id = tbl_pages.page_id
LEFT JOIN tbl_products
ON tbl_products.product_id = tbl_page_collections_products.product
WHERE
tbl_pages.page_name LIKE '%friends%'
The error I get is Unknown column 'tbl_page_collections_products.colID in on clause but I don't get that error when the subquery isn't there and that column exists in that table.
Is something conflicting?
tbl_page_collections_products is not in you subquery from clause. Maybe this is what you want:
SELECT
tbl_products.*,
GROUP_CONCAT(tags.name)
FROM
tbl_products,
(SELECT page_collection_name as name
FROM tbl_page_collections
,tbl_page_collections_products
LEFT JOIN tbl_pages ON tbl_page_collections.page_id = tbl_pages.page_id
WHERE tbl_pages.page_name LIKE '%friends%') tags
LEFT JOIN tbl_page_collections
ON tbl_page_collections.page_collection_id = tbl_page_collections_products.colID
LEFT JOIN tbl_pages
ON tbl_page_collections.page_id = tbl_pages.page_id
LEFT JOIN tbl_products
ON tbl_products.product_id = tbl_page_collections_products.product
WHERE
tbl_pages.page_name LIKE '%friends%'

Updating users table from complex SQL query, users.id not recognised?

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?