mysql query equivalent to a condition. Any suggestions? - mysql

I am not a sql expert.
I want to implement a query equivalent to this condition
If ( (first-access-of-error1 = first-access-of-error2 and
second-access-of-error1 = second-access-of-error2) OR
(first-access-of-error1 = second-access-of-error2 and
second-accessr-of-error1 = first-access-of-error2) )
I attempted it with something like:
select d.id,
concat(a.variable_name,"|",a.file_url,"|",a.line_number,"|",a.stacktrace)
as FirstAccess_Params,
concat(b.variable_name,"|",b.file_url,"|",b.line_number,"|",b.stacktrace)
as SecondA_Params
from defect d
right join (accessor a, accessor b)
on (d.id=a.defect_id and d.id=b.defect_id and a.id<b.id)
where d.category_id=0 and d.relationship_id!=-1
group by FirstAccess_Params,SecondA_Params
With above query I am able to work this condition out:
(first-access-of-error1 = first-access-of-error2 and
second-access-of-error1 = second-access-of-error2)
But I am having trouble like how to accomplish this condition:
(first-access-of-error1 = second-access-of-error2 and
second-accessr-of-error1 = first-access-of-error2)
Any help is appreciated? Any ideas are welcome..
This is schema for Accessor table
<createTable tableName="accessor">
<column autoIncrement="true" name="id" type="BIGINT UNSIGNED">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="defect_id" type="BIGINT UNSIGNED">
<constraints nullable="false"/>
</column>
<column name="operation" type="TINYINT UNSIGNED"/>
<column name="variable_name" type="VARCHAR(128)"/>
<column name="object_address" type="VARCHAR(64)"/>
<column name="type" type="TINYINT UNSIGNED"/>
<column name="module_id" type="INT UNSIGNED">
<constraints nullable="false"/>
</column>
<column name="file_url" type="VARCHAR(256)"/>
<column name="function_name" type="VARCHAR(64)"/>
<column name="line_number" type="SMALLINT UNSIGNED"/>
<column name="accessing_order" type="TINYINT UNSIGNED"/>
<column name="stacktrace_type" type="TINYINT UNSIGNED"/>
<column name="stacktrace" type="VARCHAR(2048)"/>
<column name="parameter" type="VARCHAR(5120)"/>
</createTable>
And this is schema for defect Table
<createTable tableName="defect">
<column autoIncrement="true" name="id" type="BIGINT UNSIGNED">
<constraints nullable="false" primaryKey="true"/>
</column>
<column defaultValueNumeric="0" name="rule_id" type="SMALLINT UNSIGNED">
<constraints nullable="false"/>
</column>
<column defaultValueBoolean="false" name="hide" type="BIT">
<constraints nullable="false"/>
</column>
<column defaultValueNumeric="0" name="relationship_id" type="BIGINT">
<constraints nullable="false"/>
</column>
<column name="category_id" type="SMALLINT UNSIGNED">
<constraints nullable="false"/>
</column>
<column name="sub_category_id" type="SMALLINT UNSIGNED">
<constraints nullable="false"/>
</column>
<column name="module1_id" type="INT UNSIGNED">
<constraints nullable="false"/>
</column>
<column name="module2_id" type="INT UNSIGNED"/>
<column name="execution_instance_id" type="INT UNSIGNED">
<constraints nullable="false"/>
</column>
<column name="application_id" type="INT UNSIGNED">
<constraints nullable="false"/>
</column>
<column name="project_id" type="INT UNSIGNED">
<constraints nullable="false"/>
</column>
<column name="target_id" type="INT UNSIGNED">
<constraints nullable="false"/>
</column>
<column name="testsuite_id" type="INT UNSIGNED">
<constraints nullable="false"/>
</column>
<column name="timestamp" type="INT UNSIGNED">
<constraints nullable="false"/>
</column>
<column name="priority" type="BIT"/>
<column name="status" type="BIT"/>
<column name="assignee" type="VARCHAR(64)"/>
<column name="label_ids" type="VARCHAR(1024)"/>
<column name="remark" type="VARCHAR(512)"/>
<column name="accessor_ids" type="VARCHAR(1024)"/>
<column name="parameter" type="VARCHAR(2048)"/>
<column defaultValueNumeric="0" name="parent_id" type="BIGINT">
<constraints nullable="false"/>
</column>
</createTable>

Consider this jsFiddle: http://sqlfiddle.com/#!2/c25e1/1 . It doesn't show duplicates. Also think about using DISTINCT sql command.

For the completeness of answer:
To accomplish first condition:
(first-access-of-error1 = first-access-of-error2 and
second-access-of-error1 = second-access-of-error2)
My code was sort of correct. Look at similar thing here https://stackoverflow.com/a/347300/1229355
The challenging part was to accomplish second condition:
(first-access-of-error1 = second-access-of-error2 and
second-accessr-of-error1 = first-access-of-error2)
For This: I created a new table. Though I started with the view, then moved to temporary table and then final to table. With view, I was having trouble in performance. As query was spending a lot of time in copying tmp table. With temporary table, I cannot use it in one query more then once, so it was not an option for me. So I had to stick with the table only.
Created table like this:
create table myDraceView (index(id)) as select d.id,concat_ws('|',a.variable_name,a.file_url,a.line_number,a.stacktrace) as FirstAccess_Params,"
+ "concat_ws('|',b.variable_name,b.file_url,b.line_number,b.stacktrace) as SecondA_Params from defect d right join (accessor a, accessor b) on (d.id=a.defect_id and d.id=b.defect_id and a.id<b.id) where d.category_id=0 and d.relationship_id!=-1 and d.defect_level='"
+ type
+ "' and "
+ mdaCondition;
(Sorry rushing.. have no time to write the query formally... shall do it later)
Then I did:
"select ids from (select concat( if( e1.id <= e2.id, e1.id, e2.id ),"
+ CONCAT_SEP
+ ",if( e1.id > e2.id, e1.id, e2.id ) ) as ids,e1.FirstAccess_Params as e1_FA,e1.SecondA_Params as e1_SA from myDraceView e1 join myDraceView e2 where e1.FirstAccess_Params=e2.SecondA_Params and e1.SecondA_Params=e2.FirstAccess_Params and e1.id!=e2.id group by e1.FirstAccess_Params,e1.SecondA_Params ) as l group by ids";
Any suggestions are welcome if anybody see any possible improvements that i can make.

Related

Liquibase does not create fields correctly

I have this portion of XML Liquibase script, to creates some columns in a table:
<column name="send_time" type="timestamp">
<constraints nullable="false"/>
</column>
<column name="delivery_time" type="timestamp">
<constraints nullable="false"/>
</column>
<column name="read_time" type="timestamp">
<constraints nullable="false"/>
</column>
<column name="payload" type="json">
<constraints nullable="true"/>
</column>
<column name="created_date" type="timestamp">
<constraints nullable="false"/>
</column>
<column name="created_by" type="varchar(50)">
<constraints nullable="false"/>
</column>
<column name="last_modified_date" type="timestamp"/>
<column name="last_modified_by" type="varchar(50)"/>
<column name="deleted_date" type="timestamp"/>
<column name="deleted_by" type="varchar(50)"/>
Could anyone explain why the result is like in the attached image?
In other words, this set to field 'send_time', the Default/Expression.
Why this?

Liquibase error while adding foreign keys

I am using Jhipster and developing SpringBoot and AngularJS. Jhipster is using liquibase for creating the table, loading data, update e.t.c
I am using Mysql Database.
creating table Studio
<changeSet id="00000000000002" author="Mehbub"> <createTable tableName="studioname">
<column name="studioname_id" type="bigint" autoIncrement="${autoIncrement}">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="studioname_name" type="varchar(50)">
<constraints unique="true" nullable="false" />
</column> </createTable>
</changeSet>
creating CategoryStudio table
<changeSet id="00000000000002" author="admin">
<createTable tableName="categorystudio">
<column name="categorystudio_id" type="bigint" autoIncrement="${autoIncrement}">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="categorystudio_title" type="varchar(50)">
<constraints unique="true" nullable="false" />
</column>
<column name="studio_id" type="bigint">
<constraints nullable="false" />
</column>
</createTable> </<changeSet>
Constraints:
<changeSet id="00000000000002-1" author="Mehbub">
<addForeignKeyConstraint baseColumnNames="studio_id"
baseTableName="categorystudio"
constraintName="fk_studioname_id"
referencedColumnNames="studioname_id"
referencedTableName="studioname"
deleteCascade="true"
onDelete="CASCADE"/>
</changeSet>
Creating another table "tvshow"
<changeSet id="00" author="Mehbub">
<createTable tableName="tvshow">
<column name="tvshow_id" type="bigint" autoIncrement="${autoIncrement}">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="tvshow_name" type="varchar(50)">
<constraints unique="true" nullable="false" />
</column>
<column name="movieCategory_id" type="bigint">
<constraints nullable="false" />
</column>
<column name="genreCategory_id" type="bigint">
<constraints nullable="false" />
</column>
</createTable>
</changeSet>
Contsraints:
<addForeignKeyConstraint baseColumnNames="movieCategory_id"
baseTableName="tvshow"
constraintName="fk_tvshow_movieCategory_id"
referencedColumnNames="categorystudio_id"
referencedTableName="categorystudio"
deleteCascade="true"
onDelete="CASCADE"/>
<addForeignKeyConstraint baseColumnNames="genreCategory_id"
baseTableName="tvshow"
constraintName="fk_tvshow_genreCategory_id"
referencedColumnNames="genresCategory_id"
referencedTableName="genresCategory"
deleteCascade="true"
onDelete="CASCADE"/>
</changeSet>
another table: genresCategory
<changeSet id="00" author="admin">
<createTable tableName="genresCategory">
<column name="genresCategory_id" type="bigint" autoIncrement="${autoIncrement}">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="genresCategory_name" type="varchar(50)">
<constraints unique="true" nullable="false" />
</column>
</createTable>
</changeSet>
If i am adding only one foriegn key then it's working fine.But, when i added second foriegn key, it's throing the below error.
my equation is , each category have many tv shows and each tvshows have many Genres
i am getting the error as:
liquibase.exception.MigrationFailedException: Migration failed for change set config/liquibase/changelog/00_constraints_tvShow.xml::00::Mehbub:
Reason: liquibase.exception.DatabaseException: Can't create table `allcomiclibrary`.`#sql-2ad_3ac` (errno: 150 "Foreign key constraint is incorrectly formed") [Failed SQL: ALTER TABLE allComicLibrary.tvshow ADD CONSTRAINT fk_tvshow_genreCategory_id FOREIGN KEY (genreCategory_id) REFERENCES allComicLibrary.genresCategory (genresCategory_id) ON DELETE CASCADE] at liquibase.changelog.ChangeSet.execute(ChangeSet.java:619) at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:51) at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:79) at liquibase.Liquibase.update(Liquibase.java:214) at liquibase.Liquibase.update(Liquibase.java:192) at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:431) at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:388) at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.initDb(AsyncSpringLiquibase.java:103) at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.lambda$afterPropertiesSet$0(AsyncSpringLiquibase.java:83) at io.github.jhipster.async.ExceptionHandlingAsyncTaskExecutor.lambda$createWrappedRunnable$1(ExceptionHandlingAsyncTaskExecutor.java:68) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
Can you please help me out what i am doing wrong here.
I was creating db tvshow first and then genres .. so it was hitting error .. changed the change log sequence and worked fine

Liquibase mysql store newline

I try to insert a row into mysql using liquibase in a spring-boot app. Something similar to this:
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
<changeSet author="laxika" id="20160508-2">
<insert tableName="news">
<column name="title" value="Hello world"/>
<column name="release_date" value="2016-05-09 00:00:00"/>
<column name="icon" value="update"/>
<column name="message" value="
a
b
c
d
"/>
</insert>
</changeSet>
</databaseChangeLog>
For some strange reason liquibase doesn't add newlines to the sql properly so I end up having one long string in the db. How can I force liquibase to insert with newlines?
Here is the schema for the table I'm trying to insert into:
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
<changeSet author="laxika" id="20150922-4">
<createTable tableName="news">
<column name="id" type="smallint unsigned" autoIncrement="true">
<constraints primaryKey="true" nullable="false" />
</column>
<column name="release_date" type="date">
<constraints nullable="false" />
</column>
<column name="title" type="varchar(256)">
<constraints nullable="false" />
</column>
<column name="message" type="varchar(2048)">
<constraints nullable="false" />
</column>
<column name="icon" type="varchar(32)">
<constraints nullable="false" />
</column>
</createTable>
<modifySql>
<append value="ENGINE=INNODB DEFAULT CHARSET=utf8"/>
</modifySql>
</changeSet>
</databaseChangeLog>
I tried all above answers, eventually I see an answer on the community forum of liquibase which has the answer:
<column name="message"><![CDATA[
a
b
c
d
]]></column>
Try usind CDATA in the XML :
<column name="message" value="<![CDATA[
a
b
c
d
]]>"/>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
<changeSet author="laxika" id="20160508-2">
<insert tableName="news">
<column name="title" value="Hello world"/>
<column name="release_date" value="2016-05-09 00:00:00"/>
<column name="icon" value="update"/>
<column name="message" valueComputed="(SELECT '\na\nb\nc\nd\n' from dual)"/>
</insert>
</changeSet>
</databaseChangeLog>
Worked for me:
<insert tableName="AUDIT_ATTR">
<column name="AUDIT_ID" valueNumeric="601"/>
<column name="KEY" value="FINDINGS_IN_CATEGORY_NCW_TEXT"/>
<column name="VALUE">
tra1
la2
la3
</column>
</insert>

Summarise an a XML data

I have an input in the format below which pulls all movie lists with actor names (key is movie) and I need to summarize the data on actor perspective.
<Response>
<dpDeviceName>TEST</dpDeviceName>
<dpTxnId>10412325</dpTxnId>
<dpTimespamp>2014-06-19 08:51:41 UTC</dpTimespamp>
<StoredProcedureResult>
<resultSet>
<metadata>
<columnInfo index="1" nullable="false" name="FNAME" precision="3" scale="0" type="SQL_CHAR"/>
<columnInfo index="2" nullable="false" name="LNAME" precision="30" scale="0" type="SQL_CHAR"/>
<columnInfo index="3" nullable="false" name="ID" precision="3" scale="0" type="SQL_CHAR"/>
<columnInfo index="4" nullable="false" name="MOVIE" precision="3" scale="0" type="SQL_CHAR"/>
</metadata>
<row>
<column>
<name>FNAME</name>
<value>TOM</value>
</column>
<column>
<name>LNAME</name>
<value>CRUISE</value>
</column>
<column>
<name>MOVIE</name>
<value>Jack Reacher</value>
</column>
<column>
<name>ID</name>
<value>123</value>
</column>
</row>
<row>
<column>
<name>FNAME</name>
<value>TOM</value>
</column>
<column>
<name>LNAME</name>
<value>CRUISE</value>
</column>
<column>
<name>MOVIE</name>
<value>TOPGUN</value>
</column>
<column>
<name>ID</name>
<value>123</value>
</column>
</row>
<row>
<column>
<name>FNAME</name>
<value>MORGAN</value>
</column>
<column>
<name>LNAME</name>
<value>FREEMAN</value>
</column>
<column>
<name>MOVIE</name>
<value>SEVEN</value>
</column>
<column>
<name>ID</name>
<value>128</value>
</column>
</row>
</resultSet>
</StoredProcedureResult>
</Response>
I need this to be formatted in the below manner :
<library>
<Actor>
<firstname>TOM</firstname>
<lastname>CRUISE</lastname>
<movielist>
<movie>Jack Reacher</movie>
<movie>TOPGUN</movie>
</movielist>
<id>123</id>
</Actor>
<Actor>
<firstname>MORGAN</firstname>
<lastname>FREEMAN</lastname>
<movielist>
<movie>Seven</movie>
</movielist>
<id>128</id>
</Actor>
</library>
I am stuck at the place where i have to make a reference to next row from current for each loop to decide if movie list has to continue or block has to be closed. Any guidance here is greatly appreciated.

Liquibase not working with mysql autoincrement

I try to create a new table via a liquibase changeset that looks like:
<createTable tableName="mytable">
<column name="id" type="number" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(50)"/>
<column name="description" type="varchar(255)"/>
<column name="image_path" type="varchar(255)"/>
</createTable>
this fails with following error:
liquibase.exception.DatabaseException:
Error executing SQL CREATE TABLE
kkm.mytable (id numeric AUTO_INCREMENT NOT NULL, name VARCHAR(50) NULL, description
VARCHAR(255) NULL, image_path VARCHAR(255) NULL,
CONSTRAINT PK_BOUFFE PRIMARY KEY (id)):
Incorrect column specifier for column 'id'
if I set autoIncrement="false", this works perfectly.
Is this a known issue ?
EDIT:
this is working:
<createTable tableName="mytable">
<column name="id" type="number" autoIncrement="false">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(50)"/>
<column name="description" type="varchar(255)"/>
<column name="image_path" type="varchar(255)"/>
</createTable>
<addAutoIncrement
columnDataType="int"
columnName="id"
incrementBy="1"
startWith="1"
tableName="mytable"/>
Change type="number" to type="BIGINT".
i,e
<createTable tableName="mytable">
<column name="id" type="BIGINT" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(50)"/>
<column name="description" type="varchar(255)"/>
<column name="image_path" type="varchar(255)"/>
</createTable>
Hope it works..!!!!