0

I have 2 table with different column names.

Table1

alias_id | key | keyword
1          a=1   xx
2          b=1   xxxx

Table2

product_id | store_id
1            1   
2            7

I want the query the all keyword row from the table1 where the key a=1 and in the table2 the product_id=1 and store_id=7. In the table1 the key column contains like this key_id=20 but in the table2 I have a column product_id which is contains 20

I tried a lot of option like union and join, but somehow don't want to work for me.

9
  • How does the two tables relate? Is it that Table1.alias_id == Table2.product_id ? Commented Jun 15, 2015 at 23:08
  • This is my problem, the two table is not related. So I don't know any solution is exist? Commented Jun 15, 2015 at 23:11
  • So why do you need to query both tables at once? Basically unions are used to combine result sets w3schools.com/sql/sql_union.asp and joins to combine tables using a common fields w3schools.com/sql/sql_join.asp . Commented Jun 15, 2015 at 23:16
  • I tried with union. SELECT * FROM table1 WHERE key LIKE "a=%" UNION SELECT store_id FROM table2 WHERE store_id=7 but i get error The used SELECT statements have a different number of columns. So need for me query the all keyword, but only when both criteria is exist. at table1 LIKE a=% and at table2 store_id=7 the more query is can be null Commented Jun 15, 2015 at 23:20
  • What you want exatly ?! alias_id and product_id are linking two table together ? Commented Jun 15, 2015 at 23:30

3 Answers 3

1

If you are querying two tables without any of the same fields and have a "where clause", use this:

WITH query1 as (Select t1.a from t1 where t1.a = 'xxxxx'),
     query2 as (Select t2.b from t2 where t2.b = 'yyyyy')
Select * FROM query1 FULL OUTER JOIN query2 on t1.a <> t2.b
Sign up to request clarification or add additional context in comments.

Comments

0

According to your description in comments, try this:

select key,keyword from table1 where key='a=1'
union all
select product_id,store_id from table2 where product_id='1' and store_id='7'

Comments

0

To use Union we have to make the two select statement have the same number of columns and the same data types,

Selected columns listed in corresponding positions of each SELECT statement should have the same data type.

select `alias_id`as c1, `key` as c2, `keyword` as c3
from Table1 where `key` LIKE 'a=%'
union all
select `product_id` as c1, CAST(`store_id` as CHAR(4)) as c2, '' as c3 
from Table2 where store_id=7

Live DEMO

3 Comments

What does CAST in your query ?
@stack They need to have same data type, even if there is auto cast, I put it to make clear answer.
Sorry probably i can't explain very well this. So only need for me the query if 3 criteria is exist. Table1 LIKE a=|1| Table2 product_id=|1| store_id=7 Table1 LIKE a=|1| = Table2 product_id=|1| And after need for me a row with the keyword. Maybe this is something if statement. Need to query the keyword when all criteria is meets. Both mentioned solution gave for me back 2 rows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.