I was trying the "show tables like" function described in the TDengine doc to filter out tables using pattern matches. I have multiple tables like tb_1, tb_2 ... in the database. As described in the manual each '_' matches one character so expecting "show tables like tb__" should generate the output with all tables have "tb_x" name patterns. However the output is empty. Is this command broken or the usage is incorrect in my case?
taos> show tables;
table_name | created_time | columns | stable_name | uid | tid | vgId |
==========================================================================================================================================================
tb_1 | 2021-08-25 10:58:14.668 | 4 | st | 562950003757616 | 3 | 2 |
tb_2 | 2021-08-25 10:58:14.687 | 4 | st | 562950020535164 | 4 | 2 |
...
...
...
Query OK, 100 row(s) in set (0.007341s)
taos> show tables like tb__;
Query OK, 0 row(s) in set (0.005915s)
try
select tbname from stable_name where tename like "tb__";
Related
I recently found the select syntax in TDengine SQL is slightly different from mysql due to stable. But I cannot give a reasonable explanation. Let me show the emample:
Here is the stable structure:
describe mystable;
Field | Type | Length | Note |
=================================================================================
ts | TIMESTAMP | 8 | |
value | DOUBLE | 8 | |
tag1 | NCHAR | 16 | TAG |
tag2 | NCHAR | 43 | TAG |
tag3 | NCHAR | 29 | TAG |
tag4 | NCHAR | 10 | TAG |
tag5 | NCHAR | 2 | TAG |
When I execute:
select count(*) from mystable
count(*) |
========================
270419 |
And count on a colume, the result should be the same:
select count(value) from mystable
count(value) |
========================
270419 |
However, When count on a tag, the results is different:
select count(tag1) from mystable
count(tag1) |
========================
13 |
So what does select(tag_name) actually means in TDengine sql?
After get the response of professional personnel of R&D. The select(tag_name) means number of tables belong to this stable with specified tag, we can print it out by using sql:
select tbname, tag1 from stable;
From my understanding, select count(*) or count(column) returns the number of records of each child table which belongs to the same super table. But tags are different, although tags can be used to filter records for each child table, but does not attach to each record, so select count(tag) returns number of distinct tags for stable, but not number of total records.
the query results showed in taos shell are truncated, anybody knows how to disable this truncate? I have tried to use \G, but the output format is different.
taos> select * from tb1;
ts | f1 | f2 |
=========================================================================
2022-03-31 08:50:44.398 | 1 | Hash Join (cost=230.47..71... |
Query OK, 1 row(s) in set (0.001446s)
taos> select * from tb1\G;
*************************** 1.row ***************************
ts: 2022-03-31 08:50:44.398
f1: 1
f2: Hash Join (cost=230.47..713.98 rows=101 width=488) (actual time=0.711..7.427 rows=100 loops=1)
Query OK, 1 row(s) in set (0.001340s)
you can use following sql
taos> set max_binary_display_width 100;
taos> show stables;
name | created_time | columns | tags | tables |
==================================================================================================================================================================
meters1 | 2022-05-26 18:53:58.611 | 121 | 2 | 10000 |
Query OK, 1 row(s) in set (0.001390s)
It seems \G is OK and there's no difference in your output.
You can also reference to this FAQ.
I have a master table named Master_Table and the columns and values in the master table are below:
| ID | Database | Schema | Table_name | Common_col | Value_ID |
+-------+------------+--------+-------------+------------+----------+
| 1 | Database_1 | Test1 | Test_Table1 | Test_ID | 1 |
| 2 | Database_2 | Test2 | Test_Table2 | Test_ID | 1 |
| 3 | Database_3 | Test3 | Test_Table3 | Test_ID2 | 2 |
I have another Value_Table which consist of values that need to be deleted.
| Value_ID | Common_col | Value |
+----------+------------+--------+
| 1 | Test_ID | 110 |
| 1 | Test_ID | 111 |
| 1 | Test_ID | 115 |
| 2 | Test_ID2 | 999 |
I need to build a query to create a SQL query to delete the value from the table provided in Master_Table whose database and schema information is provided in the same row. The column that I need to refer to delete the record is given in Common_col column of master table and the value I need to select is in Value column of Value_Table.
The result of my query should create a query as given below :
DELETE FROM Database_1.Test1.Test_Table1 WHERE Test_ID=110;
or
DELETE FROM Database_1.Test1.Test_Table1 WHERE Test_ID in (110,111,115);
These query should be inside a loop so that I can delete all the row from all the database and tables provided in master table.
Queries don't really create queries.
One way to do what you're saying, which could be useful if this is a one time thing or very occasional thing, is to use SSMS to generate query statements, then copy them to the clipboard, paste them into the window, and execute there.
SELECT 'DELETE FROM Database_1.Test1.Test_Table1 WHERE '
+ common_col
+ ' = '
+ convert(VARCHAR(10),value)
This probably isn't what you want; it sounds more like you want to automate cleanup or something.
You can turn this into one big query if you don't mind repeating yourself a little:
DELETE T1
FROM Database_1.Test1.Test_Table1 T1
INNER JOIN Database_1.Test1.ValueTable VT ON
(VT.common_col = 'Test_ID' and T1.Test_ID=VT.Value) OR
(VT.common_col = 'Test_ID2' and T1.Test_ID2=VT.Value)
You can also use dynamic SQL combined with the first part ... but I hate dynamic SQL so I'm not going to put it in my answer.
I have 2 different tables. I need to get a name from the TMK table in table 1 as below, and I need to bring the total number from my 2nd table. I can't write join. can u help me
TMK Table;
| tmkName |
| George |
| Jacob |
flowNewStatus Table;
|statusId|
| 1 |
| 2 |
if george has number 1 status i want this join
| tmkName | |statusId|
| George | | 1 |
Before getting to possible SQL queries... from the tables you show you'd need an additional table that associates the person to status, a join table. Essentially a TMK_status table:
TMK_status table
| personID | statusID |
|----------|----------|
| 1 | 1 |
| 2 | 3 |
| 3 | 1 |
Alternatively, the statusID could be stored as a column of TMK thus,
TMK table
| personID | tmkName | statusID |
|----------|----------|----------|
| 1 | George | 1 |
| 2 | Jacob | 3 |
If by "I can't write join", you mean you don't know how, check this answer: What is the difference between "INNER JOIN" and "OUTER JOIN"? - you will need an inner join.
If, on on the other hand, you mean you can't use join statements, then you could write a subselect statement. There could be other solutions but they depend on how you decide to join/relate the 2 tables.
I use PostgreSQL 10.1 and:
CREATE TABLE human
(
id ... NOT NULL,
gender ...,
height ...,
weight ...,
eye ...,
hair ...,
...
);
I have an input form through which I insert the data. I wish an elegant and proper way by which I can SELECT which columns required to be DISPLAYED in that form, something like weight ... DISPLAYED, or eye ... NOT DISPLAYED, .
One way is to correspond NULL with DISPLAYED (when NOT NULL then display it, or when NULL then do not display it) and use information_schema which (corresponding) makes me no so happy:
Another way is to:
CREATE TABLE human_column
(
id ... NOT NULL,
characteristic character varying(...),
is_displayed boolean
);
where characteristic data are the names of the columns of human table.
Is there a better way to add a direct foreign attribute to the columns of a table? (In 51.7. pg_attribute there is a column named attoptions. Would it be used?)
specifying "options" for columns to define if they will be "displayed" or not seems a little overhead. Imagine you keep such list in human_column. To modify it you would need to update it with new is_displayed values. Then you would need to build column list to be selected in query.
When you create a view, you do the same (specify a list of columns to be displayed) and then you can just query the view, without need to dynamically build the query. Also you can always check the current list of included columns from catalog or information_schema.
The only "not cosy" feature of a view - you can't change columns in it, thus you have to drop and create it again.
drop/create view on demand looks cheaper to me then dynamically building query with list of columns on each select still.
demo:
db=# create view v as select oid,datname from pg_database;
CREATE VIEW
db=# select * from v;
oid | datname
-------+-----------
13505 | postgres
16384 | t
1 | template1
13504 | template0
16419 | o
(5 rows)
checking list of columns:
db=# select column_name,ordinal_position,column_default,is_nullable,data_type,character_maximum_length from information_schema.columns where table_name = 'v';
column_name | ordinal_position | column_default | is_nullable | data_type | character_maximum_length
-------------+------------------+----------------+-------------+-----------+--------------------------
oid | 1 | | YES | oid |
datname | 2 | | YES | name |
(2 rows)
same for original table:
db=# select column_name,ordinal_position,column_default,is_nullable,data_type,character_maximum_length from information_schema.columns where table_name = 'pg_database';
column_name | ordinal_position | column_default | is_nullable | data_type | character_maximum_length
---------------+------------------+----------------+-------------+-----------+--------------------------
datname | 1 | | NO | name |
datdba | 2 | | NO | oid |
encoding | 3 | | NO | integer |
datcollate | 4 | | NO | name |
datctype | 5 | | NO | name |
datistemplate | 6 | | NO | boolean |
datallowconn | 7 | | NO | boolean |
datconnlimit | 8 | | NO | integer |
datlastsysoid | 9 | | NO | oid |
datfrozenxid | 10 | | NO | xid |
datminmxid | 11 | | NO | xid |
dattablespace | 12 | | NO | oid |
datacl | 13 | | YES | ARRAY |
(13 rows)