Using wildcards in SQL IN operator - mysql

Structure:
table
c1 c2
1 test
1 test2
2 test
2 test3
3 test4
SQL:
SELECT c1 FROM table WHERE c2 in ("test3","test4")
What I want:
SELECT c1 FROM table WHERE c2 in ("%3","%st4")
How can I do this?

SELECT c1 FROM table WHERE (c2 like "%3") or (c2 like "%st4")

Here is my approach just to follow the logic you have in your question:
http://sqlfiddle.com/#!9/aca65e/2
SELECT *
FROM `table`
WHERE c2 REGEXP '.*3$|.*st4$';
But keep in mind that solution
SELECT c1 FROM table WHERE (c2 like "%3") or (c2 like "%st4")
provided by #splash58 could be much faster if c2 column is indexed.

Related

How can I get the result by only one sql in MySQL

There two mysql table just like table1 and table2. I want get the result by one sql.
#table1
c1 c2 //clomun
a 10
b 20
c 30
#table2
c1 c2
a 11
b 21
e 99
I want get the result like below.
# result
c1 c2
a 21
b 41
c 30
e 99
This should work:
select c1, sum(c2) from
(
select c1, c2 from table1
union all
select c1, c2 from table2
) as total
group by c1
Please note that if the columns names are not identical, you will need to give them identical alias names as below:
select column1, sum(column2 ) from
(
select c1 as column1, c2 as column2 from table1
union all
select c1 as column1, c2 as column2 from table2
) as total
group by column1

Join two tables by row

I have two tables with same column I need to merge that two table like below
Table1
id name
1 test1
4 test7
5 test9
6 test3
Table2
id name
2 test2
3 test5
6 test3
Result
id name
1 test1
2 test2
3 test5
4 test7
5 test9
6 test3
So I need to join/merge the two tables by id and you can see id 6 present in both table I need to override table 2 value and give above result. Kindly help me to solve the issue.
Thank you.
select id,name from table1
union
select id,name from table2 ;
other way
select * from (
select id,name from table1
union
select id,name from table2)temp order by temp.id ;
This will arrange records id wise
UNION will eliminate duplicate record , In your case it's id 6
When you want sorting then must be to create inner query like this
select * from
(
select id,name from table1 t1
union
select id,name from table2 t2
)a order by a.id ;

How to fetch changed rows by comparing a table with its older version?

I have one log table and one view.
I would like to fetch the changed rows from the view by comparing it to the log table given an ID_NO.
The ID_NO is fixed between the two tables, whereas other columns can change.
In short, I would like to fetch the rows from Table1 which have one more changed columns in comparison to Table2.
for example:
TABLE 1:
ID COL1 COL2 COL3
1 A B C
2 34 56 D
3 F XY 24
TABLE 2:
ID COL1 COL2 COL3
1 A B C
2 34 56 F
3 1 XY 24
The query should return the following from TABLE2:
ID COL1 COL2 COL3
2 34 56 F
3 1 XY 24
Please advise.
Many Thanks!
SELECT *
FROM one_view vw
WHERE EXISTS
(
SELECT 1
FROM log_table t
WHERE vw.id_no = t.id_no
)
;
A note after the question was updated:
SELECT *
FROM table_2 t1
WHERE EXISTS
(
SELECT 1
FROM table_1 t2
WHERE t1.id_no = t2.id_no
AND
(
t1.col1 <> t2.col1
OR t1.col2 <> t2.col2
OR t1.col3 <> t2.col3
)
)
;
you could add a trigger to the changing table that inserts the id in a second table that is used to identify the changed rows from the changing table. Just comparing the values between tables might work but requires a lot of work. Getting the id's of the changed rows might be easier.
Just in case you also want to have the old values, add the changed colums and values to the logging table.

left join not working - get records not in other table

I feel weird, as every other answer says it works, but I can't get the correct results :(
table A: id_num, name<br>
table B: id_num
table A has index on name, but not unique. id_num is unique in this table.<br>
table B has index on id_num, but not unique.
I want to get table A names, who are NOT in table B.
This not working:
**SELECT a.name FROM a
LEFT JOIN b ON (a.id_num = b.id_num)
WHERE b.id_numb IS NULL**
its returning names that ARE in table b (and some that are not).
this didn't work either:
**SELECT distinct(a.name)
FROM a where a.id_num
not in (select distinct(b.id_num) from b )**
I can't comprehend how a.names are being return who ARE IN table B, when the SQL says NOT IN.
what am I missing?
thanks
By right, the left join should work. And as well as in.
Here is a sample. So you may want to publish and show your table schema and data for more justice on your question. Best if you could just create the tables on SQLFIDDLE and show the reference. ;)
Here is just SAMPLE:-
SQLFIDDLE reference
Sample Tables:
tab1:
COL1 COL2
1 john
2 tim
3 jack
4 rose
tab2:
COL3 COL4
1 2
2 3
3 5
4 5
5 2
Query:
select * from tab1
where col1 not in
(select distinct col4 from tab2)
;
Results:
COL1 COL2
1 john
4 rose
As per OP's updated comments and table structure
OP mentioned the table tab1 will have multiple records for same name. According to his original table design, there's NO NAME field in the table tab2. It's also much recommended if OP had provided the expected results initially.
* SQLFIDDLE Reference
OP's table data:
COL1 COL2
1 john
2 tim
3 jack
4 rose
5 john
6 john
COL3 COL4
1 2
2 3
3 5
4 5
5 2
6 6
Query: When you do not want any name that is duplicated
select t.col1, t.col2
from tab1 t
join
(select t1.col2, count(*) as counts
from tab1 t1
group by t1.col2
having count(*) = 1) x
on t.col2 = x.col2
where t.col1 not in
(select distinct col4 from tab2)
;
Results: Here is Rose, the only record that has no duplicates and do not exist in tab2
COL1 COL2
4 rose
try this
select t1.name from table1 t1
where t1.name not in
(select t2.name from table2 t2 )
look the DEMO SQLFIDDLE
EDIT:
if you have only id_num in your second table then here look to
THIS DEMO SQLFIDDLE

Condition in Select statement….. T SQL

I have a table1 with columns as C1, C2, C3 and C4
All these columns stores bit value(true or false).
How to write a select query which uses the logical operations on these columns and gets me the final result?
Ex.:
Select ((C1 OR C2) AND (C3 OR C4)) AS FinalResult
from table1
Bitwise Operators are supported for bit columns:
Select ((C1 | C2) & (C3 | C4)) AS FinalResult
from table1
When both operands are bit, the result is going to be same as if logical operators were applied.
Just test to see if it is equal to 1 (true):
Select CASE WHEN (C1 = 1 OR C2 = 1) AND (C3 = 1 OR C4 = 1) THEN 1 ELSE 0 END AS FinalResult
from table1