Setting: Trends passes name of a watch, a Value is worth (in using field or B field, retrieve) , all watches have A field or B field, whether does the list that I need judgement to be passed currently have A or B, next joining together inquires a condition, how should be the statement written, PG library
You Mei You He Shi De Han Shu Zhi Lei De , Xie Xie 。
Chinese looks so that understand not quite. Do you think the form that check Yu includes the line with some specific ? The instruction below prints a form that includes specific line renown. Select Table_name From Information_schema.columns Where Column_name = ‘your_column_name’
Java? Jdbc can get watch structure
@kalok is right, you are written so sure, this sentence that you write just realized inquiry to include all lists of this Column, what I want to come true is, when inquire, whether is Select Form TableA Where (put in existence field A in this place judgement for instance or field B, the word of existence inquires statement) on joining together, the inquiry statement that forms finally may be Select Form TableA Where A=value or Select Form TableA Where B=value, do not know the description is clear not. . . .
All watches have A field or B field, why is that judged even ” whether does the list that sends currently have A perhaps B “
StringBuilder Str =”select * From Table Where 1=1″;If()Str +=”and Name1 = Value1″;If()Str +=”and Name2 = Value2″;
Be to drip?
SELECT * From Tab_name
Where 1 In (Select Count(*) From (SELECT Column_name FROM
INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = Tab_name AndColumn_name Like A) As T);
If A exists, return the row of need, it is empty table otherwise.
The meaning of @a22271001 original poster should be all watches have A and B among them
Select * From expresses a nameWhereCase When Exists (select 1 From Information_schema.columns Where Table_name = ‘ expresses a ‘ And Column_name = ‘a’) Then A = ‘value’Else B = ‘value’ End
Here direct Select * , if the basis expresses a structure to choose correspondence to inquire I estimate the field,return so that follow stature inquiry is handled in that way in Where, cent is good into two Sql write much
@asAnotherJack #8 is written this in Mysql, pg is trashy over- , do not know have similar thing
With Sql Builder this problem is very relaxed
@duyuyouci is opposite
@asAnotherJack is this train of thought
@asAnotherJack #8 seems this to be no good, if express the word Sql that does not have B field,sign up for a fault
Create Table Test1 (a Int);Create Table Test2 (b Int);Insert Into Test1 Values (1);Insert Into Test2 Values (2);
Create Or Replace FUNCTION F(tb Text, val1 Integer)Returns TABLE
Returns TABLEVal Int
Val IntAs
$body$
BEGIN
IF EXISTS(SELECT A.attnameFrom Pg_class C, Pg_attribute A, Pg_type TWhere C.relname = TbAnd A.attnum>0And A.attrelid = C.oidAnd A.atttypid = T.oidAnd A.attname = ‘a’) THENReturn Query Execute ‘select A From ‘ | | Tb | | ‘ Where A = ‘ | | Val1;
ELSIF EXISTS(SELECT A.attnameFrom Pg_class C, Pg_attribute A, Pg_type TWhere C.relname = TbAnd A.attnum>0And A.attrelid = C.oidAnd A.atttypid = T.oidAnd A.attname = ‘b’) THENReturn Query Execute ‘select B From ‘ | | Tb | | ‘ Where B = ‘ | | Val1;
END IF;
END;
$body$Language Plpgsql;
Lz can change compactly
@MoYi123 6
Do a Map, record every watch to have A perhaps B is not good
I forge 3 kinds of plan
1.Pass the metadata of the database to express, judgement has do not have corresponding field
2.Desc is expressed or Show builds watch statement, can get all fields
3.Carry out Select A From Table_name Where 1=2 directly, if sign up for a fault, just did not have A field.
The plan of upstairs useful still function, use more convenient, but I am opposite this to not be familiar with.
@ackoly hum, the 1 that I use finally