mysql error: use result of innerjoin into new query - mysql

I have 2 sql string I want to combine:
The first table has all data of a product
The second table (inner join) has all data of products that has a group price.
But I get different result
test search 'sku'
SELECT sku
FROM `c3eiwitlive`.`catalog_product_entity`
WHERE (
`entity_id` LIKE '%17086%'
)
Direct sql on the group_price table
result 269 records
SELECT SKU, value
FROM `c3eiwitlive`.`catalog_product_entity_group_price` AS T1
INNER JOIN
`c3eiwitlive`.`catalog_product_entity` AS T2
ON T1.`entity_id` = T2.`entity_id`;
Combined sql returns less records
result 234 records
SELECT name
FROM `c3eiwitlive`.`catalog_product_flat_1` AS T1
WHERE T1.`sku`
IN (
SELECT sku
FROM `c3eiwitlive`.`catalog_product_entity_group_price` AS T1
INNER JOIN `c3eiwitlive`.`catalog_product_entity` AS T2 ON T1.`entity_id` = T2.`entity_id`
)

It look to me like there are some sku entries missing from the 'catalog_product_flat_1' table.
Why not invert the query to identify the missing rows?
SELECT sku
FROM `c3eiwitlive`.`catalog_product_entity_group_price` AS T1
INNER JOIN `c3eiwitlive`.`catalog_product_entity` AS T2 ON T1.`entity_id` = T2.`entity_id`
WHERE sku
NOT IN (
SELECT sku from `c3eiwitlive`.`catalog_product_flat_1`
)

Related

SQL query to select produts using join on specific problem

Two tables are given. In one table we have following columns
productid
product_title
priority
categoryid
and in second table we have
id
productid
color
selling_price
stock
How to write a SQL query to select all product from table having more than 2 colors?
One way would be to use a sub-query to select product id from the stock table where product has multiple colors.
SELECT PRODUCTID FROM TABLE2 GROUP BY PRODUCTID HAVING COUNT(COLOR)>2
Use it in the main query
SELECT T1.PRODUCTID,T1.PRODUCT_TITLE FROM TABLE1 T1 WHERE PRODUCTID IN
(SELECT PRODUCTID FROM TABLE2 GROUP BY PRODUCTID HAVING COUNT(COLOR)>2)
I'm assuming the Database is not DB2 in which case this query won't work.
SELECT T1.productid, T1.product_title,COUNT(T2.COLOR) counter
FROM Table1 T1 INNER JOIN table2 T2 s ON T1.productid = T2.productid
GROUP BY T1.productid, T1.product_title
HAVING COUNT(T2.COLOR) > 2;

MS Access: find rows with different fields in two tables

In MS Access:
I am trying to compare two tables with:
- TABLE1.docnumb1 = TABLE2.docnumb2
- looking for: TABLE1.sum <> TABLE2.sum2
But query retrieves an error: syntax error in from clause (or when creating left join I get an error that JOIN isn't supported):
SELECT docnumb1, sum
FROM Table1
JOIN Table2 ON docnumb1 = docnumb2;
How do I query the rows with different values?
looking to your sample (image)
you could compare the subquery for sum
select t1.rownumb, t1.sum1 -t2.sum2
from (
SELECT rownumb, sum(value) sum1
FROM Table1
group by rownumb
) t1
INNER JOIN
(
SELECT rownumb, sum(value) sum2
FROM Table2
group by rownumb
) t2 ON t1.rownumb = t2.rownumb and (t1.sum1 -t2.sum2 ) <> 0
use left join
SELECT docnumb1, sum
FROM Table1 a
left JOIN Table2 b ON a.docnumb1 = b.docnumb2 and a.value=b.value
where b.docnumb2 is null

How do I select two tables in 1 query?

I want to select two tables in one query but it doesn't seem to work. I have tried nested select but I just got sql_error:
Subquery returns more than 1 row
Here is my query:
SELECT `client`.`id` as client_id,
(SELECT `org`.`name` FROM `org`) as organization
FROM `client`
What is the better way to query two tables?
Here is my expected result:
client_id = [1,2,3,4,5]
organization = [x,y,z]
This will work but it isn't what you want i think.
SELECT `client`.`id` as client_id, `org`.`name` as organization FROM `org`, `client`
This will give you a cross product of all rows in org with all rows in client. Maybe you want something like this:
SELECT `client`.`id` as client_id, `org`.`name` as organization FROM `org` JOIN `client` ON `client`.`memberOf` = `org`.`id`
The JOIN will connect rows of both tables where column memberOf in table client is equal to column id in table org
You should use join queries to join two or more tables. you can visit https://dev.mysql.com/doc/refman/5.0/en/join.html
Example of the join query:
SELECT t1.name, t2.salary
FROM employee AS t1 INNER JOIN info AS t2 ON t1.name = t2.name;

How do I write this SQL Query to join these three tables?

I have three tables that I need to query in order to get my results.
Table 1 has an AppId and a ProjectID
Table2 has an AppID and an AppName.
Table 3 has a ProjectID and a ProjectName
I want to get out of this a list, By AppName, the ProjectNames they are tied to.
So far, a basic query to get what I want works, but I only get the ID's. I need to somehow join these to get the names associated. I need to somehow join this to table2 with the project name information, and table 2 with the appname information.
Select * from Table1 ( this table has only ID's, not names)
Order by AppId
You can join the tables like this:
Select t2.AppName, t3.ProjectName
from table1 t1
inner join table2 t2 on t2.AppID = t1.AppID
inner join table3 t3 on t3.ProjectID = t1.ProjectID

Mysql can't join two tables columns into one show

I want to show two columns summarize data.
table1 - count all fields that the id same as the id on the show_users table.
table2 - sum all values that the id same as the id on the show_users table.
This is my query:
SELECT show_users.id, COUNT(`table1`.id) as sum_fields , SUM(`table2`.count) as count_all
FROM `show_users`
LEFT JOIN `table1` ON `show_users`.id = `table1`.id
LEFT JOIN `table2` ON `show_users`.id = `table2`.id
GROUP by show_users.id
ORDER BY sum_fields DESC
The table2 results are fine, but the table1 count isn't correct values...
Why is that?
SELECT show_users.id, COUNT(DISTINCT `table1`.id) as sum_fields , SUM(`table2`.count) as count_all