MySQL IN() clause multiple returns - mysql

I have a special data environment where I need to be returned data in a certain way to populate a table.
This is my current query:
SELECT
bs_id,
IF(bs_board = 0, 'All Boards', (SELECT b_name FROM certboards WHERE b_id IN (REPLACE(bs_board, ';', ',')))) AS board
FROM boardsubs
As you can see I have an if statement then a special subselect.
The reason I have this is that the field bs_board is a varchar field containing multiple row IDs like so:
1;2;6;17
So, the query like it is works fine, but it only returns the first matched b_name. I need it to return all matches. For instance in this was 1;2 it should return two boards Board 1 and Board 2 in the same column. Later I can deal with adding a <br> in between each result.
But the problem I am dealing with is that it has to come back in a single column both name, or all names since the field can contain as many as the original editor selected.

This will not work the way you're thinking it will work.
Let's say bs_board is '1;2;3'
In your query, REPLACE(bs_board, ';', ',') will resolve to '1,2,3', which is a single literal string. This makes your final subquery:
SELECT b_name FROM certboards WHERE b_id IN ('1,2,3')
which is equivalent to:
SELECT b_name FROM certboards WHERE b_id = '1,2,3'
The most correct solution to the problem is to normalize your database. Your current system or storing multiple values in a single field is exactly what you should never do with an RDBMS, and this is exactly why. The database is not designed to handle this kind of field. You should have a separate table with one row for each bs_board, and then JOIN the tables.
There are no good solutions to this problem. It's a fundamental schema design flaw. The easiest way around it is to fix it with application logic. First you run:
SELECT bs_id, bs_board FROM boardsubs
From there you parse the bs_board field in your application logic and build the actual query you want to run:
SELECT bs_id,
IF(bs_board = 0, 'All Boards', (SELECT b_name FROM certboards WHERE b_id IN (<InsertedStringHere>) AS board
FROM boardsubs
There are other ways around the problem, but you will have problems with sorting order, matching, and numerous other problems. The best solution is to add a table and move this multi-valued field to that table.

The b_id IN (REPLACE(bs_board, ';', ',')) will result in b_id IN ('1,2,6,7') which is different from b_id IN (1,2,6,7) which is what you are looking for.
To make it work either parse the string before doing the query, or use prepared statements.

Related

Mysql JSON_EXTRACT ignore some fields when doing 'not matching' requests

I went into some trouble when performing some select JSON_EXTRACT requests on JSON data stored in a Mysql database.
Each row doesn't have exactly the same JSON data structure. All is going well when I'm using JSON_EXTRACT to select fields matching a condition.
The problem is when trying to select fields that are not matching the condition. Only fields which does have the key (though not matching data of course) are returned.
You'll find a fiddle here that reproduces this behavior.
I think it's an intended thing but I wonder if there is a sugar workaround that can lead to the fiddle's fourth request result without adding another condition (in the real case, the requests are programmatically generated based on a specific API syntax and adding contextual conditions will be a pain) ?
One way around your problem is to select id's which match the expression, and then use them in an IN or NOT IN expression dependent on whether you want to check for a match or non-match e.g.
SELECT *
FROM `test`
WHERE id IN (SELECT id
FROM `test`
WHERE data->>'$.test' = 'passed');
or
SELECT *
FROM `test`
WHERE id NOT IN (SELECT id
FROM `test`
WHERE data->>'$.test' = 'passed');
The only difference in the queries is the addition of the word NOT to negate the match.
Demo

WHERE IN with subquery NOT WORKING as expected

I am trying to get result from my categories table using the parent path i have created.
When i launch the request WHERE IN with manual data it's working perfectly.
When i am trying the same request dynamically with subquery, i got only one result instead of 4 expected.
I do not understand why, can you help me ?
http://sqlfiddle.com/#!2/88b68/6
/*Working query*/
SELECT t.id_categorie FROM t
WHERE t.id_categorie IN (1396,1399,1403,1412)
/*Not working by subquery ??*/
SELECT cat.id_categorie FROM t as cat
WHERE
cat.id_categorie IN (SELECT REPLACE(t.path,'.',',') FROM t WHERE t.id_categorie = 1412)
Thanks by advance,
Regards,
Using in with a comma delimited list does not do what you want. Even when you use a subquery. You could try this:
SELECT cat.id_categorie
FROM t cat
WHERE EXISTS (SELECT 1
FROM t
WHERE t.id_categorie = 1412 AND
find_in_set(cat.id_categorie, REPLACE(t.path, '.', ',')) > 0
);
I'm not sure what your data looks like or even what this query is supposed to be doing (it seems strange that the subquery is on the same table as the outer query). Usually, though, you want to store things in tables rather than delimited lists. You see, SQL has a built-in data structure for lists. It is called a table.
The reason this does not work is that your inner SELECT produces a single comma-separated VARCHAR, rather than four individual integers.
In MySQL you can use find_in_set, like this:
SELECT cat.id_categorie FROM t as cat
WHERE find_in_set(cat.id_categorie, (
SELECT REPLACE(t.path,'.',',')
FROM t WHERE t.id_categorie = 1412))
What I did was replacing the IN expression in your query with a call to find_in_set, leaving everything else the same.
Demo
However, this solution is fundamentally flawed, because it stores multiple values in a single column to represent a many-to-many relationship. A better approach to implementing this requirement is using a separate table that connects rows of the table to their related rows.

Query for multiple conditions in MySQL

I want to be able to query for multiple statements when I have a table that connects the id's from two other tables.
My three tables
destination:
id_destination, name_destination
keyword:
id_keyword, name_keyword
destination_keyword:
id_keyword, id_destination
Where the last one connects ids from the destination- and the keyword table, in order to associate destination with keywords.
A query to get the destination based on keyword would then look like
SELECT destination.name_destination FROM destination
NATURAL JOIN destination_keyword
NATURAL JOIN keyword
WHERE keyword.name_keyword like _keyword_
Is it possible to query for multiple keywords, let's say I wanted to get the destinations that matches all or some of the keywords in the list sunny, ocean, fishing and order by number of matches. How would I move forward? Should I restructure my tables? I am sort of new to SQL and would very much like some input.
Order your table joins starting with keyword and use a count on the number of time the destination is joined:
select
d.id_destination,
d.name_destination,
count(d.id_destination) as matches
from keyword k
join destination_keyword dk on dk.keyword = k.keyword
join destination d on d.id_destination = dk.id_destination
where name_keyword in ('sunny', 'ocean', 'fishing')
group by 1, 2
order by 3 desc
This query assumes that name_keyword values are single words like "sunny".
Using natural joins is not a good idea, because if the table structures change such that two naturally joined tables get altered to have columns the same name added, suddenly your query will stop working. Also by explicitly declaring the join condition, readers of your code will immediately understand how the tables are jones, and can modify it to add non-key conditions as required.
Requiring that only key columns share the same name is also restrictive, because it requires unnatural column names like "name_keyword" instead of simply "name" - the suffix "_keyword" is redundant and adds no value and exists only because your have to have it because you are using natural joins.
Natural joins save hardly any typing (and often cause more typing over all) and impose limitations on join types and names and are brittle.
They are to be avoided.
You can try something like the following:
SELECT dest.name_destination, count(*) FROM destination dest, destination_keyword dest_key, keyword key
WHERE key.id_keyword = dest_key.id_keyword
AND dest_key.id_destination = dest.id_destination
AND key.name_keyword IN ('sunny', 'ocean', 'fishing')
GROUP BY dest.name_destination
ORDER BY count(*), dest.name_destination
Haven't tested it, but if it is not correct it should show you the way to accomplish it.
You can do multiple LIKE statements:
Column LIKE 'value1' OR Column LIKE 'value2' OR ...
Or you could do a regular expression match:
Column LIKE 'something|somtthing|whatever'
The trick to ordering by number of matches has to do with understanding the GROUP BY clause and the ORDER BY clause. You either want one count for everything, or you want one count per something. So for the first case you just use the COUNT function by itself. In the second case you use the GROUP BY clause to "group" somethings/categories that you want counted. ORDER BY should be pretty straight forward.
I think based on the information you have provided your table structure is fine.
Hope this helps.
DISCLAIMER: My syntax isn't accurate.

query returning multidimensional grouped arrays

In the hypothetical scenario where I have a couple of database tables, in a one to many relationship. I am trying to find a nice query function so that I can return each row in table A with a nested array for all the linked rows from table B.
SELECT a.id AS id,a.name as name,b.id AS b_id,b.name AS b_name FROM a,b WHERE a.id=b.eid;
will return only one result
SELECT
a.id AS id,
a.name as name,
GROUP_CONCAT(b.id) AS b_id,
GROUP_CONCAT(b.name) AS b_name
FROM a,b
WHERE a.id = b.eid;
whereas this returns what I want but I'm after an array not a string (there might be commas in b.name)
What am I missing?
You can use separator different from comma:
GROUP_CONCAT(b.name SEPARATOR ';') AS b_name
It isn't possible to return anything other than a MySQL datatype, so the short answer is no you cannot return a array (nested or otherwise) of results from a subquery. You'll also see some pretty drastic performance hits trying to build string concatenated values from the foreign, the sub-string order will be implicit and, as you've alluded to, you'll have problems selecting a suitable delimiter.
It almost certainly going to be more efficient use a simple JOIN to create a single result set. Any duplicates on the "one" side of the relationship should be handled programmatically (which is preferable, in any sense, to handling string splitting on multiple concatenated fields).

Mysql Joins - How to know which row is retrieved from which table...?

Consider i am using join on three tables to get a desired result-set...
Now in that result set, is there anyway that i can find out which row comes from which table...?
Update :
I see that i have phrased the question rather wrongly.... As pointed in one of the answers below, a result-set returned by join may will contain a row made up columns from multiple talbes...
So the question should actually be "Consider i am using union on three tables to get a desired result-set..."
You can add a table identifier column to each:
select 'A' as table_name, col1, col2 from A
union
select 'B' as table_name, col1, col2 from B
union
...
This returns a single result set which is handled by your application as any ordinary select statement:
while ( rows available ) {
row = fetchrow
if row.table_name == 'A' then
do special handling for table A
else if row.table_name == 'B' then
do special handling for table B
else if ...
}
The actual syntax is dependent on the language you are using, but most procedural languages follow the scheme above.
If you are asking this question, then your database is probably not structured correctly. (correctly being a subjective term).
A proper SQL query on a normalized database should not depend, nor be concerned with, where the data comes from.
Each row would be a combination of all tables, with null values being inserted in columns for left/right/outer joins which do not match the joining criteria. You could perhaps test if a column (from a particular table) is null, and derive from that that the non-null values must originate from the opposite table(s).
Then again, if you were actually performing an UNION, as Marcelo suggested, you would have to look at ancillary columns to determine the source of the data, as that information is lost in the combination.