I need some help please.
I have this simple mysql query:
select '1,2,3,4,5,6' as Col1 from dual;
and the table1:
Col1
1,2,3,4,5,6
And I have another table2:
service_id service_name
1 Service1
2 Service2
I tried next query but not working:
select service_name from table2 where service_id in (select col1 from table1)
Any help would be appreciated!
Try this:
Sample data:
create table `dual` (Col1 varchar(100));
insert into `dual` values ('1,2,3,4,5,6');
create table table2 (service_id int, service_name varchar(100));
insert into table2 values
(1, 'Service1'),
(2, 'Service2');
T-SQL:
select service_name from table2 t2
where exists(
select 1 from `dual` d
where locate(concat(',', t2.service_id, ','), concat(',', d.Col1, ',')) > 0
);
I'm not sure exactly what you're trying to accomplish, but I can give you a couple hints.
Your "IN" clause would only work if your service_id was '1,2,3,4,5,6'. I can see what you're trying to do, but the database is treating the result from col1 as the whole string of numbers including all the commas, not the individual numbers themselves. If you hard-coded it as "WHERE IN (1,2,3,4,5,6)", you would be getting matches. Instead of using the "IN" clause, you could JOIN the tables and use LIKE.
Try This:
SELECT
table2.service_name
FROM
table2
LEFT OUTER JOIN
table1
ON
table1.col1 LIKE CONCAT('%', table2.service_id, '%')
WHERE
table1.col1 IS NOT NULL
I think that will do what I think you want to do.
Related
I have a database with 2 tables. table1 and table2.
Table1 contains a list of tests ( column 'name' ). And I want to count how many rows are missing in table2 ( this table has also a column 'name' ) according to the list of tests of table1.
So I just want to count the mismatch between table1.name and table2.name.
I tried several querys, but all didnt really work.
I tried to use the 'NOT IN' statement but it takes too much time. Like several minutes.
For example, the output should be :
COUNT(*) = 20
It means that 20 tests are missing ( or not done yet ) in table2.
I'm using MySQL, so I can't use EXCEPT or MINUS statement.
Thank you by advance.
Nordine
You can use not exists :
select count(*)
from table1 t1
where not exists (select 1 from table2 t2 where t2.name = t1.name);
If you have a duplicate name in table1 then you need count(distinct t1.name) instead.
Try the below query:
select count(case when bname is null then 1 end)
from
(
select a.name as aname, b.name as bname from
table1 a left join table2 b
on a.name=b.name)x
MINUS can be used in MySQL.
Ref:http://www.mysqltutorial.org/mysql-minus/
Try this:
SELECT name
FROM table1
MINUS
SELECT name
FROM table2
I'm not sure what I'm doing wrong but I have my tables. I know this question has been asked a million times but I can't seem to figure out why it's only pulling a portion of the transaction ID's from one of my tables.
I have two tables. author and tab2.
author looks like so and the unique id is COL9:
COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 COL9
data data data data data data data data 6314085733
My other table is tab2 and there is only one column in it:
COL1
6300798484
6300917409
6301563169
UPDATE: I just realized that there are two spaces in quiet a few of the COL1 fields. I changed the data type to varchar(10) to eliminate those extra spaces and reran the query.. still nothing.
My query is like so:
SELECT b.Col1, a.*
FROM author a
JOIN tab2 b
ON b.COL1 = a.COL9
ORDER BY a.COL9 DESC
I know there has to be more than 600 and the results I'm getting are:
Showing rows 0 - 24 (29 total, Query took 7.2141 sec) [COL9: 6319720972 - 6302432564]
You need to use TRIM, like this:
SELECT b.Col1, a.*
FROM author a
JOIN tab2 b
ON TRIM(b.COL1) = TRIM(a.COL9)
ORDER BY a.COL9 DESC
If COL1 and COL9 holds numeric values use a cast to get the data.
SELECT b.Col1, a.*
FROM author a
JOIN tab2 b
ON CAST(b.COL1 AS BIGINT) = CAST(a.COL9 AS BIGINT)
ORDER BY CAST(a.COL9 AS BIGINT) DESC;
Or you can use a TRIM.
Trim is not by default available in mssql.
first create a Scalar valued function.
CREATE FUNCTION [dbo].[Trim]
(
-- Add the parameters for the function here
#pString VARCHAR(2000)
)
RETURNS VARCHAR(2000)
AS
BEGIN
RETURN LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(#pString, CHAR(10), ''), CHAR(13), ''), CHAR(9), '')));
END
GO
Use the below query
SELECT b.Col1, a.*
FROM author a
JOIN tab2 b
ON dbo.Trim(b.COL1) = dbo.Trim(a.COL9)
ORDER BY dbo.Trim(a.COL9) DESC;
I am having problem with SQL. I want to get the different records from two different tables with different number of columns, which have the same primary key.
I have tried "EXCEPT" and "MINUS", but since the two tables have different number of columns, I received errors.
Is there any other sql commands that I can use to get the records that I want?
Thanks,
Min
You can use INNER JOIN Like:
SELECT table1.column1, table2.column3
FROM table1 INNER JOIN table2
ON table1.column1=table2.column1;
A FULL OUTER JOIN with key columns equal to NULL conditions can help you solve your issue.
Here is the sample SELECT statement
/*
create table Tab1(id int, col1 varchar(10))
create table Tab2(id int, col2 varchar(10), col3 int)
insert into tab1 select 1,'Kodyaz'
insert into tab1 select 2,'Vader'
insert into tab2 select 2,'Star Wars', 6
insert into tab2 select 3,'SQL', 2014
*/
select *
from tab1
full outer join tab2 on tab1.id = tab2.id
where
tab1.id is null
or tab2.id is null
I hope it helps,
Try this:
SELECT t1.table1column1,
t1.table1column2,
t1.table1column3,
t2.table2column1,
t2.table2column2,
t2.table2column3
FROM table1 t1
JOIN table2 t2 ON t1.table1column1 = t2.table2column1
i have a db like this:
Table1:
id
id_item
table (enum: 'table2','table3','table4')
table2:
id
value
table3:
id
value
table4:
[...]
And i want to run a query like this:
SELECT t1.id, t2.value FROM table1 AS t1
LEFT JOIN table1.table as t2 ON t1.id_item=t2.id
Is it possible? or i have to select first table1 and after the value?
( sorry for my bad eng :) )
If I understood the last column in Table 1 correctly and it is just a string, you can't.
You cannot write a column into the FORM-clausal and wait for mysql to evaluate it for every row and find the correct table to join it with.
To do this you will need to create a view where you will have the data from all the tables, together with the table name as an additional column. Afterwards you can perform a join like that between Table1 and the new view
I have a complex select query from table1 that returns 2 columns of data (id and value)
I want to update columns id and value of table2 with that data.
How can i do it ?
I have tried something like this
update table2 set (id, value) values (select ....)
and other things, but no results :P
NOTE & EDIT: I've seen about UPDATE INNER JOIN but the problem is that my select is complex ... it's like
select distinct(colA), sum(case statement....) as c1, sum(case statement...) as c2 from table2 group by colA
and colA, c1 and c2 is what i want to update in other table
thank you