I am trying to use a with as clause in a query.
WITH NAME AS
(
SELECT col1,
col2
FROM TABLE1
)
SELECT col2,
col3
FROM TABLE2
WHERE col2 in (NAME.col1)
The query placed in the with as clause works on it's own but I get this error:
Unknown column NAME.'col1' in 'where clause'
I am more familiar with oracle so this may have caused some error by carrying something over.
Is the order of the clauses wrong and With As comes after where?
I think you do not need the CTE.
SELECT col2,
col3
FROM TABLE2
WHERE col2 in (SELECT col1 FROM TABLE1)
should work
If you want to use CTE (for what reason ever), you have to select from the table
WITH NAME AS
(
SELECT col1,
col2
FROM TABLE1
)
SELECT col2,
col3
FROM TABLE2
WHERE col2 in (select col1 from name)
Related
I have 2 tables: tbl1 and tbl2. I want to return a single row from tbl1 with columns: col1, col2, col3, can_be_deleted, have_important_items. The idea is that can_be_deleted and have_important_items columns are boolean values resulted (both) by searching in the same table tbl2.
SELECT
col1,
col2,
col3,
NOT EXISTS(SELECT 1 FROM tbl2 WHERE mycategory=10 AND status>0 LIMIT 1) AS can_be_deleted,
EXISTS(SELECT 1 FROM tbl2 WHERE mycategory=10 AND type_item>0 AND status>0 LIMIT 1) AS have_important_items
FROM tbl1 WHERE ... LIMIT 1
To avoid later clarifications, tbl2 columns are:
mycategory - a value to group items inside table
status - enabled/disabled item
type_item - 0-not important, >=1 important one (scale of importance)
Question: Can I write a faster query?
using JOIN is faster than EXIST,check if this query solve your probleme and if not then just change the conditions to get your result.
"t1.t2_id" is the foreign key of tbl2, change it to the correct name
SELECT
col1,
col2,
col3,
LEFT JOIN tbl2 AS t2 ON t2.id = t1.t2_id
WHERE t2.mycategory = 10
AND t2.status>0
AND t2.type_item>0
FROM tbl1 AS t1 WHERE ... LIMIT 1
i am performing a sub query in mysql which is like
select col1, col2 , (select col3 from table2) as 'data'
from table1
where not data is null
how should i get data in where clause. IS it POSSIBLE
One way to do this is :
SELECT *
FROM (
select col1, col2 , (select col3 from table2) as 'data'
from table1
)t
WHERE data IS NOT NULL
As you see there I have created on derived table t for your query, now result of your query is treated as Table(temp table) and having columns as col1,col2 and col3, Using this result set we can able to access col3 in where clause .
Note - assuming that select col3 from table2 returns single value as per OP's comments
Use cross join:
select t1.col1, t1.col2, t2.col3 as data
from table1 t1 cross join
(select col3 from table2) t2
where t2.col3 is not null;
I want to count the number of times a grouping occurs in a query.
SELECT COL1,
COL2,
*(count COL1)*
FROM TABLE
GROUP BY COL1, COL2
So I'm expecting to see the results like so:
COL1, COL2, Count
A, A, 1
A, B, 2
B, A, 1
B, B, 2
B, C, 3
Count(*) will give me the sum of the grouped row which is what I don't want, but I just cant seem to get my head round it or find a way to do it. Anyway, thanks in advance.
You should add auto_increment column to make an order. After that, try something like this:
SELECT a.col1, a.col2,
(SELECT Count(*) From T1 c
WHERE c.col1 = a.col1
AND c.ID <= a.ID) as count
FROM T1 a;
SQL Fiddle
OR Without auto_increment Try this:
SELECT col1, col2,
(select count(*) from T1 t2
where t2.col2 <= t1.col2 and
t2.col1 = t1.col1
) as Enumeration
FROM T1 t1
GROUP BY col1, col2;
SQL Fiddle
Your query (with the parentheses appropriately placed) does what you want:
SELECT COL1, COL2, count(*)
FROM TABLE
GROUP BY COL1, COL2;
If you want the sum of a column, then use the sum() function.
EDIT:
If you are trying to enumerate the groups (despite what the question is asking in rather clear English), you can do:
SELECT col1, col2,
(select count(*)
from table t2
where t2.col1 = t.col1 and
t2.col2 <= t.col2
) as Enumeration
FROM table t1
GROUP BY col1, col2;
I have to make a table out of two other tables (and use union). The query that works is:
SELECT * FROM tabel1
UNION
SELECT * FROM tabel2
Now what i have to do is put this result(data) into table3 (a table i already have, with the same columns as in table1 and table2).
Who can help me?
INSERT INTO table3
SELECT * FROM tabel1
UNION
SELECT * FROM tabel2
since you have the same columns in all three of them ...
In a general case you should work with column lists like
INSERT INTO table3 (col1, col2, col3)
SELECT col1, col2, col3 FROM tabel1
UNION
SELECT col1, col2, col3 FROM tabel2
This way you avoid trouble with auto_increment id-columns. Also You should consider using UNION ALL since UNION filters out duplicate lines and therefore will take longer on large tables.
is it possible to do conditional select based on a number of rows in a stored procedure?
E.g. if select * from table1 has no records, then do select * from table2?
Try this:
SELECT col1, col2, ..., coln FROM table1
UNION ALL
SELECT col1, col2, ..., coln FROM table2
WHERE NOT EXISTS (SELECT * FROM table1)
This of course assumes that both tables have exactly the same structure.