I have a mysql table with a column that looks something like this:
| TAGS |
------
|Green |
|Blue |
|Orange|
|Blue |
|Green |
| ... |
------
Now what I want to do is output all the different tags that exist in a list, BUT every tag can only be outputted once (so e.g. 'Green stands two times in the database but can only stands one time in the list')
Hope you understand my question!
Thanks
You said "row" but do you mean a column labeled "tags" in your table?
There are two different ways:
Method 1:
SELECT DISTINCT tags FROM table_name WHERE condition;
Method 2:
SELECT tags FROM table_name WHERE condition GROUP BY tags;
They both will return a you an array where each item in the row is the tag without repeats (distinct). The main difference is that DISTINCT makes it easier to optimize (and possibly quicker).
Now, if you didn't make a typo and said that you have a row with a column that has multiple tags like:
|ROW_ID|TAGS |
| 1 |'Blue', 'Red' |
| 2 |'Red', 'Yellow' |
| 3 |'Blue', 'Black', 'Red'|
Then you'll have to do some parsing and array operations (but that's a completely different answer).
Related
I have a table in which each column represent a text from a manuscript, here is a simple example:
mss1 | mss2 | mss3
------------------------
The | The | A
big | big | big
black | |
dog | dog | dog
I would like to display rows where two columns have different values (or the same values), for instance I want to see the differences between mss1 and mss3. The result should look like:
mss1 | mss3
---------------
The | A
black |
These seemed to be good solution candidates :
SELECT mss1, mss3 FROM table WHERE mss1 != mss3;
SELECT mss1, mss3 FROM table WHERE mss1 NOT LIKE mss3;
However it is not working, even after converting all columns from text to varchar of the same length.
I also tried LOCATE (See here) to find same values: if I can locate mss1 in mss3 and vice-versa, they must have the same value, right? But that was not successful either.
Any idea? It seems like it should be easy, but I can't figure it out...
Perhaps the issue with your query is that NULL values are filtered out. For your purposes, I think this may do what you want:
SELECT mss1, mss3
FROM table
WHERE coalesce(mss1, '') <> coalesce(mss3, '');
My table looks something like this:
| id (int) | sentence (varchar) |
I want to find all rows that are almost the same except for one particular word. Eg:
| 230 | test |
| 321 | test sth |
...
| 329 | is (sth) it?
| 923 | is it?
The word that can be different is sth in this case. Ideally I could use some sort of "array" with the list of words that can be different.
Is this something I could do purely in SQL?
Just an untested quick shot, sorry, but I think you could do something like
SELECT * FROM table GROUP BY REPLACE(text, 'sth', '')
You can use SOUNDEX. So with the examples that you gave, these queries:
SELECT SOUNDEX('test')
SELECT SOUNDEX('test sth')
SELECT SOUNDEX('is (sth) it?')
SELECT SOUNDEX('is it?')
return these results:
T230
T230
I200
I200
That means that the first two and the second two sound like each other. What I can't be sure of is how well this will work with your actual data, you're just going to have to try it.
I am trying to work with a two (already) generated tables.
In one table 1 there is a column named column_code
In table 2 the columns are named like the data in column_code
Per example
Table 1:
meta_data
----------------------------------
| pid | 1 |
| question | favorite website |
| column_code | AA123ZZ |
----------------------------------
where column_code has a content named: 'AA123ZZ'
and question is called: what is your favorite website
Table 2:
content
-----------------------------------
| id | 4 |
| submit_date | 14-01-2013 |
| AA123ZZ | stackoverflow.com |
-----------------------------------
The content of this table_name AA123ZZ is for instance "stackoverflow.com"
Now what I want is a query where the result will be something like:
id: = 1
what is your favorite website: stackoverflow.com
So actually AA123ZZ stands for this question, I have no other way of matching these together, how can I do this?
I am sorry I cannot make this any clearer.
I got these two tables and indeed they seem to be missing something.
So to try make this a bit clearer.
In the content table the table name AA123ZZ is 'code' for the question.
The maker of the database should just have placed the question as column name in stead of this code AA123ZZ. Now I need to match this code to what is inside meta_data.
So in meta_data in the question_column you can find: "favorite website"
and in the column_code you will find AA123ZZ.
in the content table in the AA123ZZ column you will find: stackoverflow.com
As you can see I cannot match these easily on a inner join.
So is there a way to match a column_name from one table to the contents of a column in another table?
You can't join tables where the column name itself is variable. You're going to need to fix the schema here, perhaps to include a foreign key value to the meta_data table from the content table and from there you can do a simple join on the two tables.
Read more on foreign keys
Lookup table - unique row identity
The other lookup tables just do not make sense as from what I have seen giving a row an ID then putting that id in another table which also has a id then adding these id's to some more tables which may reference them and still creating a lookup tables with more id's (this is how all the examples I can find seem) What I have done is this :
product_item - table
------------------------------------------
id | title | supplier | price
1 | title11 | suuplier1 | price1
etc.
it then goes on to include more items (sure you get it)
product_feature - table
--------------------------
id | title | iskeyfeature
1 | feature1 | true
feature_desc - table
-----------------------------
id | title | desc
1 | desc1 | text description
product_lookup - table
item_id | feature_id | feature_desc
1 | 1 | 1
1 | 2 | 2
1 | 3 | 3
1 |64 | 15
(as these only need to be referenced in the lookup the id's can be multiples per item or multiple items per feature)
What I want to do without adding item_id to every feature row or description row is retrieve only the columns from the multiple tables where their id is referenced in the same row of the lookup table. I want to know if it is possible to select all the referenced columns from the lookup row if I only know the item_id eg. Item_id = 1 return all rows where item_id = 1 with the columns referenced in the same row. Every item can have multiple features and also every feature could be attached to multiple items , this will not matter if I can just get the pattern right in how to construct this query from a single known value.
Any assistance or just some direction will be greatly appreciated. I'm using phpmyadmin, and sure this will be easier with some php voodoo I am learning mysql from tutorials ect and would like to know how to do it with sql directly.
Having a NULL value in a column is not the major concern that would lead to this design - it's the problem with adding new attribute columns in the future, at which MySQL is disgracefully bad.
If you want to make a query that returns everything about an item in one row, you need to LEFT OUTER JOIN back to the product_lookup table for each feature_id. This is about every 10th mysql question on Stack Overflow, so you should be able to find tons of examples.
my ms access table like this:
ID | Group | Detail(A) | Detail(B)
1 | A | ABC |
2 | A | DEF |
3 | B | | GHI
How can my access sql select Detail(A) as 'Details' when Group=A, Detail(B) as 'Details' when Group=B ?
Thanks
You can use immediate if, IIF.
SELECT IIf(Group="A",DetailA,DetailB) As Detail
FROM Table
I like Remou's answer, the IIF is a good simple function, however if you are comparing multiple values, it could quickly grow to fit all the IIF's, as an alternative in a multi scenario or even for singles values if you wish you can use the Switch method:
SELECT Switch(Group="A", DetailA, Group="B", DetailB) AS Detail
FROM Table
Then you would simply keep adding e.g. Group="C", DetailC etc