I have a little problem with some strange strings in one's database table.
I need to split such examples of strings to arrays of INT or separetely to INT looping or sth.
This must be done in 'usual' MySQL (no functions, declares etc - only SELECT statements + built-in-functions like REPLACE(), SUBSTRING())
524;779; 559;; ;559; 411;760;; + others;
Is such intention possible to perform?
If this is a run-once operation to convert your data into a sane format, you could take the idea from this answer and extend it to the maximum number of ids in one string.
Example on SQL Fiddle.
SELECT DISTINCT id, val
FROM (
SELECT id, substring_index(substring_index(`val`,';',-1),';',1) AS val FROM tab
UNION ALL
SELECT id, substring_index(substring_index(`val`,';',-2),';',1) FROM tab
UNION ALL
SELECT id, substring_index(substring_index(`val`,';',-3),';',1) FROM tab
UNION ALL
SELECT id, substring_index(substring_index(`val`,';',-4),';',1) FROM tab
UNION ALL
SELECT id, substring_index(substring_index(`val`,';',-5),';',1) FROM tab
) x
WHERE val <> ''
ORDER BY id
Related
Just wanted to ask if this is possible or a way to determine the strings that is not on my table. for example
select name from table_person where name IN('name1','name2','name3')
scenario is name1 and name2 is available on my table but what I want to display is name3, since I want to know what are the things I haven't added.
Just playing around with the worst approach (may be).
Not Recommended
SELECT
suppliedArgTable.name
FROM
(
SELECT 'name1' AS name
UNION
SELECT 'name2'
UNION
SELECT 'name3'
) AS suppliedArgTable
LEFT JOIN
table_person TP ON TP.name = suppliedArgTable.name
WHERE TP.name IS NULL;
http://sqlfiddle.com/#!9/edcbe/2/0
NOT IN combined with reversing your query is a solution.
With the 'list' ('name1','name2','name3') in a (temporary) table e.g. temp_list
and with the data in table_person
the query would be:
select name from temp_list
where name not in (
select distinct(name) from table_person
)
distinct removes doubles. (see also MySQL: Select only unique values from a column)
SELECT name_field
FROM (VALUES('name1'),
('name2'),
('name3'),
('name4'),
('name5')) V(name_field)
EXCEPT
SELECT name_field
FROM name_table
You can use a temporary table to hold a list of all names and find non-matching names with EXCEPT.
I cannot create a virtual table for this. Basically what I have, is a list of values:
'Succinylcholine','Thiamine','Trandate','Tridol Drip'
I want to know which of those values is not present in table1 and display them. Is this possible? I have tried using left joins and creating a variable with the list which I can compare to the table, but it returns the wrong results.
This is one of the things I have tried:
SET #list="'Amiodarone','Ammonia Inhalents','Aspirin';
SELECT #list FROM table1 where #list not in (
SELECT Description
FROM table1
);
With only narrow exceptions, you need to have data in table form to be able to obtain those data in your result set. This is the essential problem that all attempts at a solution to this problem run into, given that you cannot create a temporary table. If indeed you can provide the input in any form or format (per your comment), then you can provide it in the form of a subquery:
(
SELECT 'Amiodarone' AS description
UNION ALL
SELECT 'Ammonia Inhalents'
UNION ALL
SELECT 'Aspirin'
)
(Note that that exercises the biggest of the exceptions I noted: you can select scalars directly, without a base table. If you like, you can express that explicitly -- in MySQL and Oracle, at least -- by selecting FROM DUAL.)
In that case, this should work for you:
SELECT
a.description
FROM
(
SELECT 'Amiodarone' AS description
UNION ALL
SELECT 'Ammonia Inhalents'
UNION ALL
SELECT 'Aspirin'
) a
LEFT JOIN table1
ON a.description = table1.description
WHERE table1.description IS NULL
That won't work. the variable's contents will be treated as a monolithic string - one solid block of letters, not 3 separate comma-separated values. The query will be parsed/executed as:
SELECT ... WHERE "'Amio.....rin'" IN (x,y,z,...)
^--------------^--- string
Plus, since you're just doing a sub-select on the very same table, there's no point in this kind of a construct. You could try mysql find_in_set() function:
SELECT #list
FROM table1
WHERE FIND_IN_SET(Description, #list) <> ''
For example, I have a Database called db, and there are 100 tables in it called ta_1 ... ta_100 respectively. Each table has two columns id(int) and val(int).
If I want to get the overall mean of val, what should I do?
If I want to get a table as a result with two column: tablename(String) and mean(float), what should I do?
It goes without saying that you should not have set up your database this way. There are a number of solutions, ranging from easy but mundane, to hard but useful.
At some point, you have to create a list of tables, and prepare a statement that merges them together. Copy&Past using a database view would be a good start:
create view GlobalView (
tabname varchar(10),
id int,
val int)
as
select 'ta_1', id, val from ta_1
UNION ALL
select 'ta_2', id, val from ta_2
UNION ALL
select 'ta_3', id, val from ta_3
UNION ALL
.....
UNION ALL
select 'ta_100', id, val from ta_100
Then, you can simply run the select:
select avg(1.0 * val) as mean
from GlobalView
The alternative would be to create a dynamic query that you can call either from an external program, or by using 'dynamic SQL'
Is it possible to join Two queries of mysql in a query ??
Like:
select * from a + select * from b
So that I can use them in a single php loop.
If they have the same number of columns and the datatypes are the same in each column, then you can use a UNION or UNION ALL:
select *
from a
UNION ALL
select *
from b
If you provide more details about the tables, data, etc, then there might be another way of returning this data.
A UNION will return only the DISTINCT values, while a UNION ALL selects all values.
If this is the route that you need to take, and you still need to identify which table the data came from, then you can always create a column to identify which table the data is from , similar to this:
select *, 'a' TableName
from a
UNION ALL
select *, 'b' TableName
from b
This allows you to distinguish what table the data came from.
I think it is easier creating sql "variables" like:
select varA, varb from TableA, tableB;
and you can just play with values in PHP accessing properties.
That way you can take conditions in the query like:
select varA, varb from TableA, tableB where varA.id = varB.foreingId bla bla...
;)
I need a Query that without any changes work on these three different database server : MySQL, MSSQL, PostgreSQL .
In this query i have to to calculate a column with the following expression that work correctly on MySQL :
COUNT(DISTINCT field_char,field_int,field_date) AS costumernum
The fields in the distinct are of different type :
field_char = character
field_int = integer
field_date = datetime
The expression is inside a parent query select, so if i try to achieve the result with a sub query approach, i stumble in this situation :
SELECT t0.description,t0.depnum
(select count(*) from (
select distinct f1, f2, f3 from salestable t1
where t1.depnum = t0.depnum
) a) AS numitems
FROM salestable t0
I get an error with this query, how can i get the value of the parent query ?
The expression work correctly on MySQL but i get an error when i try to execute it on Sql Server or PostgreSQL (the problem is that the count function doesn't accept 3 arguments of different type on MSSQL/PostgreSQL), is there a way to achieve the same result with an expression that work in each of these database server (SQL Server, MySQL, PostgreSQL ) ?
A general way to do this on any platform is as follows:
select count(*) from (
select distinct f1, f2, f3 from table
) a
Edit for new info:
What if you try joining to the distinct list (including the dept) and then doing the count? I created some test data and this seems to work. Make sure the COUNT is on one of the t1 columns - otherwise it will mistakenly return 1 instead of 0 when there are no corresponding entries in t1.
SELECT t0.description, t0.depnum, count(t1.depnum) as 'numitems'
FROM salestable t0
LEFT JOIN (select distinct f1,f2,f3,depnum from salestable) t1
ON t0.depnum = t1.depnum
GROUP BY
t0.description, t0.depnum
How about concatenating?
COUNT(DISTINCT field_char || '.' ||
cast(field_int as varchar) || '.' ||
cast(field_date as varchar)) AS costumernum
Warning: your concatenation operator may vary with RDBMS flavor.
Update
Apparently, the concatenation operator portability is question by itself:
String concatenation operator in Oracle, Postgres and SQL Server
I tried to help you with the distinct issue.