Single query to get data from two tables with different columns - mysql

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!

Related

How to "duplicate" rows in a specific table(while auto-assigning a new ID)?

I need to create an identical copy of many records in a table. The table has a PK id, which of course will be different, in the freshly-copied records.
For example, let's say i have a scrum_card table, with the following non-unique columns: name, description, board_id
I have a dynamic array of id's of records, which i wish to duplicate: [34,56,32,3445,...]
How do i tell MYSQL, to fetch the data from all those records, and make a batch-insert of those same records?
In "human" syntax it would look something like this: "Select all columns(besides id) from scrum_card where the id's are [array of id's], then duplicate each found record".
Use INSERT INTO ... SELECT... and in the list of columns do not include the id:
insert into scrum_card(name, description, board_id)
select name, description, board_id
from scrum_card
where id in (34,56,32,3445,...)
You can use INSERT using a SELECT result set as the source, instead of a set of literal row tuples with VALUES(...).
INSERT INTO new_table (id, col1, col2, col3...)
SELECT NULL, col1, col2, col3...
FROM old_table
WHERE id IN (34,56,32,3445,...)
Using NULL in place of the id column in the SELECT will return NULL for each row, which will cause new_table to generate a new id value.
But SQL does not have any way to do a wildcard like "all columns except id," besides you typing the column names in.

Advanced trigger with SELECT inside CASE instruction

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

how to find out a result set by which column ? if we write 3 columns in where clause with OR Operators on single table

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.

Conditionally execute query

Is there a way to execute a query, depending on the result of a certain column?
My table has a column col1, which could be NULL. I want to check first. If col1 is not NULL, execute another query, if col1 is NULL, do nothing (or return something else).
In pseudocode it could look like this:
IF (SELECT col1 IS NUT NULL FROM `tab1`)
THEN (SELECT col1, col2, col3 FROM `tab1`)
PS: I execute those queries from PHP, so it would also be possible, to check the result of col1 with PHP, though I would prefer to use plain SQL.
the where clause is your if
select col1, col2, col3 FROM `tab1`
where col1 IS NOT NULL

DELETE Difference NOT IN vs NOT EXISTS

I have two scenarios represented below, SCENARIO 1 works as well as SCENARIO 2 but are both those SCENARIOS achieving the Same Objective, Note in both Scenario's otherTbl is static
SCENARIO 1
CREATE TABLE `tbl`(
col1 VARCHAR(255),
PRIMARY KEY(col1)
) ENGINE='InnoDb';
Here is my set of queries that I run previously that make sense and run fine.
#Create an exact copy of the `tbl`
CREATE TEMPORARY TABLE `tmp_tbl`( .. SAME AS `tbl` .. );
#Add grouped records from another table into `tmp_table`
INSERT INTO tmp_tbl SELECT col1 FROM otherTbl GROUP BY col1;
#Delete the tables that donot exist any more int the `otherTbl`
DELETE FROM tbl WHERE tbl.col1 NOT IN (SELECT col1 FROM tmp_tbl);
SCENARIO 2
In this scenario the difference is only of the columns, As you can see all of them are primary Keys
CREATE TABLE `tbl`(
col1 VARCHAR(255),
col2 VARCHAR(255),
col3 VARCHAR(255),
PRIMARY KEY(col1, col2, col3)
) ENGINE='InnoDb';
Here are the new set of Queries
#Create an exact copy of the `tbl`
CREATE TEMPORARY TABLE `tmp_tbl`( .. SAME AS `tbl` .. );
#Add grouped records from another table into `tmp_table`
INSERT INTO tmp_tbl
SELECT col1, col2, col3 FROM otherTbl GROUP BY col1, col2, col3;
#Delete the tables that donot exist any more int the `otherTbl`
DELETE FROM tbl WHERE NOT EXISTS(SELECT col1, col2, col3 FROM `tmp_tbl`);
The question simply is, Do they achieve the same conclusion HENCE if we replace the delete query from NOT IN to NOT EXISTS in SCENARIO 1 it will still work the same way.
******SIMPLE VERSION******
Is:
DELETE FROM `tbl` WHERE tbl.col1 NOT IN (SELECT col1 FROM tmp_tbl);
Equall To:
DELETE FROM `tbl` WHERE NOT EXISTS(SELECT col1 FROM `tmp_tbl`);
I haven't tested it, but they are most likely not equivalent. The NOT EXISTS form would make sense if used with a correlated subquery. But your subquery doesn't contain any reference to the outer query, so probably the second form won't delete any rows at all.
Also, presence of NULLs in the table may make these two forms act very differently.
These two queries should, to my knowledge, achieve the same results (since the query checks for the same data - only the second one does it in a more elegant manner maybe).