I'm doing trigger and depending on data inserted, I want to compare the input of data to select.
I have 3 columns col1, col2 and col3 (only one of them can be not null). When I insert data to table, I want to check if data joined to that column is correct.
I tried CASE but it didn't work.
CASE
WHEN col1 IS NOT NULL THEN SELECT * FROM XXX
WHEN col2 IS NOT NULL THEN SELECT * FROM YYY
WHEN col3 IS NOT NULL THEN SELECT * FROM ZZZ LEFT JOIN aaa ON col3.id=aaa.id
ELSE NULL
END
Related
I have a database table. That table has 4 columns. In 3 columns members(values) want to access the 4th column value.
So here i don't want to write same query for every member. I want to write only single query. So is it possible with single query? If possible how I can know which column has given those result set?
select 4thcolumn from tablename where lstcolumn=?1 or 2ndcolumn=?2 or 3rdcolumn=?3;
Using OR is a solution (but that requires you to repeat the parameter three times):
SELECT col4 FROM mytable col1 =:myvalue OR col2 =:myvalue OR col3 = :myvalue;
One solution to shorten the query (and pass a unique parameter) is to use IN:
SELECT col4 FROM mytable WHERE :myvalue IN (col1, col2, col3)
If you want to know which column matched, then this gets longer. In MySQL you can do:
SELECT
col4,
col1 = :myvalue is_col1,
col2 = :myvalue is_col2,
col3 = :myvalue is_col3
FROM mytable
WHERE :myvalue IN (col1, col2, col3)
This adds three columns in the result set: is_col1, is_col2, is_col3; the column(s) that matched will have value 1, other(s) will show 0.
Table mytable in mysql has columns col1 and col2 with integer not null values. It also has a primary key id. After arranging the columns in a certain order, I need to calculate a certain sum from all the elements of columns col1 and col2 as follows: For a given value in col2 in a given row, compare it with the value of col1 in the next row. Take the smaller of these two values and subtract from it the value of col1 in the row where the value of col2 was taken; for the last row just subtract from the value in col2 the value in col1 in that row. Get the sum of all these differences.
I have the following solution which seems to work but might disturb where the user lacks privileges to create tables or alter the table structure. It would require checking existence of a table and some columns before they are created, besides needing to clean up after running, to remove the columns added.
alter table mytable add my_id int(5) not null;
alter table mytable add col3 int(7) not null;
SET #count = 0;
UPDATE mytable SET my_id = #count:= #count + 1;
create temporary table mytable2 as select my_id, col1 from mytable limit 1, 99999;
update mytable a join mytable2 b on a.my_id = (b.my_id - 1) set a.col3 = b.col1;
update mytable set col3 = col2 order by my_id desc limit 1;
select sum(LEAST(col2,col3) - col1) from mytable;
The Question: What would be a better way of getting the desired sum described above?
Here is some sample data:
id col1 col2
--------------------
11 609 618
42 620 628
23 624 630
34 627 637
Required Sum: (618-609) + (624-620) + (627-624) + (637-627) = 26
I have two tables, event and performer and each of them has a slug column. In addition to the slug column, the event table has columns col1, col2 and col3 and the performer table has a column called id.
What I need is a single query that takes a slug value as input and looks at the event table first. If it finds a match, it returns all the columns from the event table. If it can't find a match, then it looks at the performer table and returns just the id.
In essence, it is the equivalent of the following two queries:
select col1, col2, col3 from event where slug = ?
If there are no results in the first query run the following:
select id from performer where slug = ?
I understand the the number of returned columns should be consistent so the value for id can be null in the first case and the values for col1, col2 and col3 can be null in the second case. I can test for null to see which was the case.
I would rather not have conditionals in the query - I have a feeling that it can be done with a single query, but can't figure out how.
You could try the IF EXISTS which can all be done in one query. I know you said no conditionals, but this is an in query command.
IF EXISTS(select col1, col2, col3 from event where slug = ?)
BEGIN
select col1, col2, col3 from event where slug = ?
END
ELSE
BEGIN
select id from performer where slug = ?
END
I hope this helped!
Given the query...
UPDATE
Table1
SET
col1 = col1 + 1,
col2 = col1 * 2
WHERE
colID = 1
...if col1 equals 1 before the query is executed, will col2 be set to 2 or 4 after the query is executed?
Just give it a try. But here you go:
http://sqlfiddle.com/#!2/7b6de/1
Col2 updates to 4.
Good luck.
Using MySQL while retrieving, and processing a data file, I m trying to see records from various columns in one row for an ID so that it ignore null values of other columns and show all values in a single row
Columns col1, col2, col3, col4 with null values for a unique row ID
For this retrieved data table
col1 col2 col3 col4
row1 1 null null null
row2 null 2 null null
row3 null null 3 null
row4 null null null level1
finalRow 1 2 3 level1
Select ID, IFNULL(col1, col2, col3, col4 From table t
Brings error message. MAX function doesn't work for me as at time MIN values need to be get pickup as well.
if I understand right, this might be a solution:
Select ID, IFNULL(col1, IFNULL(col2, IFNULL(col3, col4))) from ...
Given your example MAX will work. In your example there is only one non-null value per column so the maximum value is also the minimum. So, this will get your expected result... which I think is the finalRow:
select max(col1), max(col2), max(col3), max(col4) from table
It would be helpful to provide a better explanation of what you're looking for and also adding more examples.