Text matching order by relevance - mysql

In MySQL is there a way we can order the results in order of set of rules also used for filtering the results?
E.g. if we have a name field and input from the user then I would like to filter and sort the records as following
If we match the input against the name field
Exact Match
1st keyword of input exact match for name
2nd keyword of input exact match for name
Name starts with 1st keyword
Name starts with 2nd keyword
I would like to filter the results based on the above mentioned cases and order them in above listed sequence.
Input : two three
Data :
| one |
| two |
| three |
| one two three |
| one three two |
| three one two |
| two three one |
Output :
| two |
| three |
| two three one |
| three one two |
Full Text Search might a possible solution but from my past experience it does not always give an expected relevancy value to exactly order in the above mentioned manner.

Related

Count keys not containing atleast one value in a one to many relationship

My apologies for the nonsense example, the actual data i'm working with cannot be shared.
I have a set of customer baskets containing items forming a one to many relationship.
In BO this results in a table with the basket ID duplicated for each item within that basket.
e.g.
| ID | Item |
|----|--------|
| 1 | Apple |
| 2 | Apple |
| 2 | Orange |
| 3 | Apple |
| 3 | Orange |
| 3 | Pear |
I'm attempting to count the number of baskets that do not contain a pear, however the formulas
=Count([ID]) Where (Not([Item] ="Pear")) and =Count([ID]) Where (Count([Item]="Pear")<1) Both return incorrect results as the initial count can find other rows in the table where basket 3 does fit the conditions.
Is there a formula I can use to check all items relating to basket before counting to return my desired output of "2". I'm trying to avoid breaking up my Data query if at all possible.
Cheers,
The basic concept is to duplicate your query with additional criteria to only return the baskets with pears, left join to it, and count the IDs that are in your original, but not in the duplicated query.
Begin by duplicating your query adding criteria so you only get the baskets with pears.
Next, merge the ID dimensions for both queries.
Create a variable with the Qualification set to "Detail", the Associated dimension as the Merged ID dimension, and the formula as the Item from your duplicated query.
Finally, create a Var Number of Baskets With No Pears variable to count the IDs from your original query where the detail variable you just created is null
=Count([Query 1].[ID]) Where (IsNull([Var Query 2 Item]))
There you have it.

VB.NET selecting multiple columns of MySql table simultaneously into lists

I have a MySQL table that looks like this (attached below).
I want to simultaneously select (in a single SQL Query) multiple columns (id, Last Name, and username) and import into them into different lists of strings the values if they meet a certain condition (in this case, where color="blue".
+----+------------+-----------+----------+----------------+
| id | First Name | Last Name | Username | Favorite Color |
+----+------------+-----------+----------+----------------+
| 1 | John | Smith | jsmith | Blue |
| 2 | Avery | Nelson | aNelson | Red |
| 3 | Jack | Brooklyn | jBrook | Blue |
| 4 | Arnold | Nam | aNam | Blue |
| 5 | Charlie | Smith | cSmith | Orange |
+----+------------+-----------+----------+----------------+
... Continued
I am trying to select all the required data that meet the condition where color=Blue with the MySQL query of SELECT id, Last Name, username FROM `myTable` WHERE color="Blue". Once this data is selected, I want to import each selected column that meets the color requirement into separate lists.
For example, list FirstName should be list of strings "John, Jack, Arnold" (in that order) and Username list should contain "jsmith, jBrook, aNam" etc. In the end, I want to be able to produce three lists that contain these values which meet the Favorite Color condition in the MySQL database of Blue. How can I do this?
I know that I can make three separate reader queries but I want to do them all in one to save time.
You have not posted VB code so I'm not sure how you intend to use the results but you could load the whole table to a Datatable (unless it is really big), then use Dataview.RowFilter to filter your data with different criteria in-memory, without making any further request to the database backend.
You can see an example in VB.net here
Then you could use LINQ for example to generate a list of string from a datatable or view, see here for an example.

Mysql where in multiple rows

I have this problem.
One table with.
id | routename | usersid |
1 | route 1 | 1,2,3,5 2 |
2 | route 2 | 5,20,15 3 |
4 | route 4 | 10,15,7,5 |
I need, search ej. userid 5 in colum usersid... but I have no idea how to do, because there are multiple rows.
If you cannot change the schema then you will have to use the REGEXP operator to match on a regular expression. For example
where column REGEXP '(^|,)5(,|$)'
This matches the number 5 either at the beginning or end of the field or surrounded by commas (or any combination thereof), to avoid matching other numbers like 15, 55 or 1234567890.
If the table is large this will perform very slowly as it will require a full table scan
You might be looking for FIND_IN_SET().
select * from Table1
WHERE FIND_IN_SET(5,usersid)
SAMPLE FIDDLE

SSRS - Count grouped by Column AND Row in a Tablix

I have an SSRS tablix similar to this:
+----------+-----------+--------+--------+
| Total | RowGroup | Group1 | Group2 |
+----------+-----------+--------+--------+
| Perdiod1 | RowGroup1 | Value | Value |
| | RowGroup2 | Value | Value |
| Perdiod2 | RowGroup1 | Value | Value |
| | RowGroup2 | Value | Value |
| Perdiod3 | RowGroup1 | Value | Value |
| | RowGroup2 | Value | Value |
+----------+-----------+--------+--------+
Now, for each period, I want to calculate the count in each group. When I do:
Count(TableValue.Value, "Perdiod")
I get the total for the period (for both column groups). When I do
Count(TableValue.Value, "ColumnGroup")
I get the total for all periods. So really, what I need to do is something like this:
Count(TableValue.Value, "TablixRow", "TablixColumn")
which obviously doesn't exist.
So 'Value' should be a total count for a group within a Period (therefore, in the example given above, Value would be repeated twice in each Period (once for each RowGroup))
Is there a way to display the count of all the values within a specified column and row group in an SSRS's tablix?
You'd just need to add the aggregate expression to the matrix detail field without a specified scope, e.g. something like:
=Count(Fields!Value.Value)
Since you don't supply a scope, the aggregate will be calculated in its current scope, which for those value fields will be the particular group/period combination.
Edit after comment/update
Hmm... I'm trying to think of an easy way to do this, but coming up empty handed... I mucked around and could get any combination except the one you want.
Basically you're looking for something like:
=Count(Fields!Value.Value, "ParentRowGroup", "CurrentColumnGroup")
and I can't think of an effective way to do this.
Honestly (and it wouldn't be the first time for SSRS) the path of least resistance here is to add the period count you want to display as an extra column to your DataSet when you generate it, then just display this value unaggregated in the detail of the Matrix.
Annoying, but if you can control the DataSet it's a trivial solution to what is a difficult problem at the report level.
=CountRows()
=CountRows("GroupByInitial")
=CountRows("GroupByInitial",Recursive)

show by example: row vs field?

Various documents define row as being synonymous with record. Unfortunately, a record can be a list or a single item. All the same, a row usually contains more than one item but is sometimes called "a single entry".
A field can be a container (for example, in html), which could be considered a place to input one item, or it could be considered a place where many items are entered (albeit on different occasions, sometimes).
It sure would be nice if someone could put it in simple terms. For example, a row is the result of a single-item, single-field entry via an insert statement. A field represents all rows and intersects a column.
Can anybody provide a clear answer because google just isn't cutting it. Thank you.
Edit:
In excel it sure is cut-and-dry. Column is all horizontal. Row is all vertical. Cell is a row-column pair; a single entry. Even though relational database languages are like working with multiple spreadsheets (tables), the column, row, cell approach seems to make the most sense.
I am looking at various different explanations that don't seem to agree with one another in the answers. Can we operationally define the terms for the tutorial I am presently working on, which is not clear? Link: http://zetcode.com/databases/sqlitetutorial/introduction/#about
If this is a table...
O--O-------------O-------------O
|ID| my_col_1 | my_col_2 |
O--O-------------O-------------O
|0 | fskdjfh | jfkhgdkfj |
|1 | NULL | hfkjsdh |
|2 | jfkdhsdkjh | NULL |
|3 | fdfhkjh | NULL |
|4 | NULL | NULL |
O--O-------------O-------------O
This is a row...
|0 | fskdjfh | jfkhgdkfj |
And this is a field...
| jfkhgdkfj |
Hows that?
Imagine you have to describe many aspect of the same thing. You have to choose which aspects you want to take care of: these are the columns of your table. For each of the column you can choose the data type to represent (numeric, String, ...). The column could be also a composite data type (ex: date) or a reference to another object.
The description of an object consists in all the values contained in the columns relative to that object: this is the row/record (the two terms have the same meaning in ER databases).
The field is the value assumed by a column--let's say that it is a cell in a table. It is part of the row, but it may have no sense outside the context provided by the row and the columns.
Maybe the confusion is due to the fact that to simplify the notation, the term field is used as the term column. When you see a query like "select * from foo where somefield=something" it means that you select the rows in which the field relative to the column "somefield", assumes the value "something".
This definition is ok also with HTML fields. A field of a form is the place where you enter the value the column will have in your case, that means, in the row that represents you.
One row/record, one column/field:
+---+
| x |
+---+
One row/record, three columns/fields:
+---+---+---+
| x | y | z |
+---+---+---+
Three rows/records, one column/field:
+----+
| x1 |
+----+
| x2 |
+----+
| x3 |
+----+
Three rows/records, three columns/fields:
+----+----+----+
| x1 | y1 | z1 |
+----+----+----+
| x2 | y2 | z2 |
+----+----+----+
| x3 | y3 | z3 |
+----+----+----+
fields or columns are defined with the table, and are the part of table structure, they hold information vertically. they describe the records or rows in the table.
Example: name, age, salary etc.
Rows or records are the real data that is stored in the table, these records are the actual information. one horizontal row represent one record.
Example: 'John', 23, 23000.00