我要選擇從表1,其中有史密斯在列1列2或和狀態1.MySQL的複雜查詢
如果沒有史密斯在第1列的所有行,然後選擇從價值第2列,如果第2列中有史密斯,則在該行的第1列中選擇值。
然後,選擇所有從表2的行其包含在列1或表2中的列2該值,(我們得到了通過從表1中選擇)
1
A
回答
0
這應包括#2分之1 ,你已經失去了我#3
select if(col1 = 'smith', col2, col1) from table1
where (col1 = 'smith' or col2 = 'smith') and status = 1
0
與本公司查詢
select * from table2
where column1 in (select if(column1 = 'smith', column2, column1) from table1 where (column1 = 'smith' or column2 = 'smith') and status = 1)
OR
column2 in (select if(column1 = 'smith', column2, column1) from table1 where (column1 = 'smith' or column2 = 'smith') and status = 1)
或
select * from table2 where
column1 in (select column1 from table1 where column2 = 'Smith' AND status = 1) OR
column1 in (select column2 from table1 where column1 = 'Smith' AND status = 1) OR
column2 in (select column1 from table1 where column2 = 'Smith' AND status = 1) OR
column2 in (select column2 from table1 where column1 = 'Smith' AND status = 1)
0
select *
from table1 as t1, table2 as t2
where t1.status = 1
and (t1.col1 = 'smith' and (t2.col1 = t1.col2 or t2.col2 = t1.col2)
or t1.col2 = 'smith' and (t2.col1 = t1.col1 or t2.col2 = t1.col1))
0
也許是最好的sollution將重新設計你的數據庫, 但如果你真的想保持你的表,你可以試試這個查詢:
SELECT IF(t1.col1 = 'smith', t1.col2, t1.col1) AS t1col2, IF(t2.col1 = t1col2, t2.col2, t2.col1) AS t2col2
FROM table1 AS t1
JOIN table2 AS t2 ON(IF(t1.col1 = 'smith', t1.col2, t1.col1) IN (t2.col1, t2.col2))
WHERE (t1.col1 = 'smith' OR t1.col2 = 'smith') AND t1.status = 1
0
像這樣的東西可能是你在...
SELECT *
FROM table_1
WHERE col_1 LIKE '%smith%' AND status = 1
UNION DISTINCT
SELECT *
FROM table_1
WHERE col_2 LIKE '%smith%' AND status = 1
UNION
SELECT *
FROM table_2
WHERE ...er...
在這一點上我停止能夠理解的問題,因爲從Que問題1和問題2不是一個值,而是一個結果集。但它可能會繼續...
WHERE col_1 IN (SELECT col_1
FROM table_1
WHERE col_1 LIKE '%smith%' AND status = 1
UNION
SELECT col_2
FROM table_1
WHERE col_2 LIKE '%smith%' AND status = 1);
相關問題
- 1. MySQL複雜查詢
- 2. Mysql複雜查詢
- 3. MySQL複雜查詢
- 4. MySQL複雜查詢
- 5. 複雜的MySQL查詢?
- 6. Mysql的php複雜查詢
- 7. MySQL的複雜查詢
- 8. 複雜的MySQL查詢?
- 9. 複雜的MySQL查詢
- 10. 查詢mysql的複雜
- 11. MySQL的last_query_cost複雜查詢
- 12. 複雜的MySQL COUNT查詢
- 13. mysql中的複雜查詢
- 14. 複雜MySQL的Select查詢
- 15. 複雜的Mysql查詢 - PHP
- 16. 複雜的MySQL查詢
- 17. Mysql的複雜查詢
- 18. MySQL查詢優化複雜的查詢
- 19. mySQL複雜多項查詢
- 20. MySQL查詢複雜ORDER BY
- 21. 複雜MySQL查詢它是
- 22. MYSQL移調查詢複雜
- 23. Mysql複雜查詢加入
- 24. mySQL複雜SELECT查詢?
- 25. MySQL查詢緩存,複雜SQL查詢
- 26. 的CakePHP/PHP的MySQL的複雜查詢
- 27. MySQL的複雜的選擇查詢
- 28. MySQL的:複雜的聚合查詢
- 29. 複雜的MySQL查詢通訊隊列
- 30. MySQL:簡化複雜的查詢
不是一個複雜但複雜的優化。 – 2010-06-22 06:11:27