Is there an operand/function/command in MySQL similar to the EXCEPT operand in SQL Server?
EXCEPT returns any distinct values from the left query that are not also found on the right query.
This statement should give me the distinct values.
SELECT * FROM table1
EXCEPT
SELECT * FROM table2;
How can this be achieved in MySQL?
The best you could do is use a NOT EXISTS. Something like:
SELECT DISTINCT *
FROM table1
WHERE NOT EXISTS(SELECT NULL
FROM table2
WHERE table1.x = table2.x)
Related
I get data from MySQL with if statement as code below. but I get an error:
Error Code: 1241. Operand should contain 1 column(s).
so can someone can help me?
I can not put it into the procedure, because I am using spring + mybatis in the project and i will put this code into them.
SELECT IF((SELECT COUNT(*) FROM table1 WHERE someField = 'A') < 0,
(SELECT * FROM table2 WHERE someField = 'A'),
(SELECT * FROM table1 WHERE someField = 'A'))
From the comments: I wanna get data of 1 in 2 tables when table 1 don't have data I will get data from table 2.
That sounds like union all and not exists:
select * from table1 where someField = 'A'
union all
select * from table2 where not exists(select 1 from tabe1 where someField = 'A'
Note that, for this to work, both tables must contain exactly the same number of columns, whith aligned datatypes. You should really be enumerating the columns that you want to show in the resultset in both unioned queries, to avoid any possible ambiguity. If needed, you can cast columns or add litteral values to any or both of the resultset to align the resultsets (without seeing your actual data structures, I cannot tell how to do).
I have a table field named category_ids (text) which saves another table's ids as "1,2,3".
Now i want to use this category_ids to a sql IN() query. the query will be like tab1.category_id IN (select category_ids from tab2). but i'm facing issue as select category_ids from tab2 returns '1,2,3' so IN() query not working.
Is there any simple way to convert '1,2,3' to ('1','2','3') or (1,2,3) in sql?
You may use FIND_IN_SET here:
SELECT *
FROM tab1
WHERE FIND_IN_SET(tab1.category_id,
(select category_ids from tab2));
This is just a sample query, your actual one may differ. But the point is that if you want to search for '1' inside a CSV string '1,2,3', then there is a way to do it.
As others have already mentioned, you should avoid storing CSV data in your tables. When I see FIND_IN_SET being heavily used there is usually a smell.
try
tab1.category_id IN (select REPLACE(category_ids, '''', '') from tab2)
If table2 has more than one row, you can use exists with find_in_set():
select t1.*
from table1 t1
where exists (select 1
from table2 t2
where find_in_set(t1.category_id, t2.category_ids) > 0
);
Note that this is a very poor data structure. You should have a single row for each category id in table2. If you did, then the query would be simpler and have better performance.
Is it possible to make a query that changes the where clause acording to some condition? For instance I want to select * from table1 where data is 19/July/2016 but if field id is null then do nothing, else compare id to something else. Like the query bellow?
Select * from table1 where date="2016-07-19" if(isnull(id),"",and id=(select * from ...))
Yes. This should be possible.
If we assume that date and id are references to columns in (the unfortunately named) table table1, if I'm understanding what you are attempting to achieve, we could write a query like this:
SELECT t.id
, t.date
, t....
FROM table1 t
WHERE t.date='2016-07-19'
AND ( t.id IS NULL
OR t.id IN ( SELECT expr FROM ... )
)
It would also be possible to incorporate the MySQL IF() and IFNULL() functions, if there's some requirement to do that.
As far as dynamically changing the text of the SQL statement after the statement is submitted to the database, no, that's not possible. Any dynamic changes to the SQL text would need to be done when the SQL statement is generated, before it is submitted to the database.
My personal preference would be to use a join operation rather than a IN (subquery) predicate.
I think you're trying too hard. If id is NULL that's equivalent to having a FALSE in the where clause. So:
Select * from table1 where date="2016-07-19" and id=(select * from ...)
Should only match the records you want. If id is NULL you get nothing.
Okay, just was debugging something and I found a wired error, probably it should be documented somewhere, but I search MYSQL documentation and didn't found anything. Here is the sql query, that produce 1064 error near * from table,
Select char_length(zip), zip, * from tbllocations
But this below one works fine:
Select *, char_length(zip), zip from tbllocations
Cannot we use * at end of column list? I test this query on MySQL 5.5.41 and MySql 5.0.95. I didn't notice this error before. I rewrite query to avoid special/hidden characters.
Aren't they same query except change in order of columns? Any explanation or resource to look for one.
EDIT:
Just run Select char_length(zip), zip, tbllocations.* from tbllocations and it works fine? So looks like I am hitting some bug? or anything logical, I am missing?
From MySQL docs:
A select list consisting only of a single unqualified * can be used as shorthand to select all columns from all tables:
SELECT * FROM t1 INNER JOIN t2 ... tbl_name.* can be used as a qualified shorthand to select all columns from the named table:
SELECT t1.*, t2.* FROM t1 INNER JOIN t2 ... Use of an unqualified * with other items in the select list may produce a parse error. To
avoid this problem, use a qualified tbl_name.* reference
SELECT AVG(score), t1.* FROM t1 ...
I'm using phpmyadmin to test out some MySQL queries. I'm trying to write a larger, nested query, which is failing due to an unrecognized table alias, so I'm trying to debug smaller parts of it. However, I'm getting confusing errors when I try to use table aliases sometimes.
Can you explain why some of these queries throw errors?
SELECT * FROM table1 AS tablealias1 (works)
SELECT * FROM table1 GROUP BY userid (works)
SELECT * FROM table1 GROUP BY userid AS tablealias1 (error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS tablealias1
LIMIT 0, 25' at line 1 )
SELECT * FROM table1 WHERE userid=1 (works)
SELECT * FROM table1 WHERE userid=1 AS tablealias1 (same error as above)
(SELECT * FROM table1 WHERE userid=1) AS tablealias1 (same error as above)
You alias things to:
rename the column display's name
give it a reference name for later use elsewhere in the query statement (whether you use it explicitly or implicitly--as long as it could be used elsehere)
If you're not doing either, an alias makes no sense. You can't alias a result set unless it's used inside a subquery, then you need an alias to reference it.
This will work:
Select * FROM (SELECT * FROM table1 WHERE userid=1) AS tablealias1
as it implies
Select tablealias1.* FROM (SELECT * FROM table1 WHERE userid=1) AS tablealias1
Alone, this is garbage:
(SELECT * FROM table1 WHERE userid=1) AS tablealias1