Match multiple columns with same value in ODBC - odbc

Hi I have an Access Table like this.
----------------------------------------------------------------
| firstname | surname | address |
----------------------------------------------------------------
| Joan | Rivers | 123 Fake St. |
| Michael | Jackson | 69 Balls Head St. |
| Justin | Bieber | None |
----------------------------------------------------------------
I'm wondering if it is possible, over ODBC, to construct a query that allows me to match my input to any column.
Something like this:
SELECT * FROM NEMESISES WHERE '%value%' LIKE firstname or surname or address;
and when value is plugged in for example: '%bie%', it outputs the Justin Bieber row or when '%st%' is plugged in it outputs the Joan Rivers and Michael Jackson row.
Thank You!

You can divide it into 3 matchings:
SELECT * FROM NEMESISES
WHERE firstname LIKE '%value%'
OR surname LIKE '%value%'
OR address LIKE '%value%';
Or you can match joined values of columns:
SELECT * FROM NEMESISES
WHERE firstname || surname || address LIKE '%value%';
I would prefer first solution: database have less to do.

Related

SQLite, how can I find a value that in my sentence?

For example, a table like this:
name | age | gender
alice | 23 | female
bob | 21 | male
irfan | 24 | male
......
I get a sentence like "Hi, bob and alice!", I want to know the sentence if include the name in the table and return them. What should I do?
Like:
"Hi, bob and alice!" -> return the bob's value and alice value in the table
How to write SQL statements?
Something like
SELECT *
FROM yourtable
WHERE 'Hi, bob and alice!' LIKE '%' || name || '%';
is a start.

Get weight of words by occurence

Maybe this is related to math.stacexhange, but I am affraid, that I will get a formula in answer what I won't undersand.
I have products in our database, and I have products from different suppliers in another table.
What I want is to pair, these supplieres products to our products if it is possible, or show for me at least show me a list, where the matching is high.
I did iterate throught all the suppliers products, and explodes the product name by spaces, and store it in a table, and the count of the occurence.
The table seems like this.
+--------+-------------+---------------+-------+
| id | word | originalWord | count |
+--------+-------------+---------------+-------+
| 220950 | Tracer | Tracer | 493 |
| 220951 | Destroyer | Destroyer | 3 |
| 220952 | Avago5050 | Avago5050 | 4 |
| 220953 | mouse | mouse | 2535 |
| 220954 | TRAMYS44916 | /TRAMYS44916/ | 2 |
| 220955 | GameZone | GameZone | 16 |
| 220956 | Enduro | Enduro | 3 |
| 220957 | AVAGO | AVAGO | 10 |
| 220958 | 5050 | 5050 | 4 |
| 220959 | optical | optical | 2370 |
| 220960 | USB | USB | 6160 |
+--------+-------------+---------------+-------+
and so on. Of course, in another table I stored, what is the product id for each word.
So what I want is to determine the weight of a word by occurence.
As you see, the word TRAMYS44916 is occured only twice, almost certain that is a partnumber, so this is the most heavy word. It weight should be 1.
Let's say the most occured is USB with 6160 occurence, so it weight should be like 0.01 or something like that, I think.
What is the best way to get all the weights of the words?
There are other tables for other suppliers so dispersion is always change.
This reminds me of Naive Bayes text classification, so to determine which product should it belongs to, you can calculate tf-idf of all the words.
Then if you want to pair it from another product name, you can decompose it to words again and select the product id based on the highest term value, however maybe you should specify some threshold for this, because in some cases it would not be that clear.
tf-idf = ("number of word matches in product name"/"word count of product name") * log ("number of products" / "number of products that contains the word")
You can see how it is done in the example here (In your case the document will be the product full name): https://en.wikipedia.org/wiki/Tf–idf#Example_of_tf.E2.80.93idf
Example implementation in Java: https://guendouz.wordpress.com/2015/02/17/implementation-of-tf-idf-in-java/

sqlite, order by date/integer in joined table

I have two tables
Names
id | name
---------
5 | bill
15 | bob
10 | nancy
Entries
id | name_id | added | description
----------------------------------
2 | 5 | 20140908 | i added this
4 | 5 | 20140910 | added later on
9 | 10 | 20140908 | i also added this
1 | 15 | 20140805 | added early on
6 | 5 | 20141015 | late to the party
I'd like to order Names by the first of the numerically-lowest added values in the Entries table, and display the rows from both tables ordered by the added column overall, so the results will be something like:
names.id | names.name | entries.added | entries.description
-----------------------------------------------------------
15 | bob | 20140805 | added early on
5 | bill | 20140908 | i added this
10 | nancy | 20140908 | i also added this
I looked into joins on the first item (e.g. SQL Server: How to Join to first row) but wasn't able to get it to work.
Any tips?
Give this query a try:
SELECT Names.id, Names.name, Entries.added, Entries.description
FROM Names
INNER JOIN Entries
ON Names.id = Entries.name_id
ORDER BY Entries.added
Add DESC if you want it in reverse order i.e.: ORDER BY Entries.added DESC.
This should do it:
SELECT n.id, n.name, e.added, e.description
FROM Names n INNER JOIN
(SELECT name_id, description, Min(added) FROM Entries GROUP BY name_id, description) e
ON n.id = e.name_id
ORDER BY e.added

manyToMany connection add existing entity or create a new one

Imagine connection Person x Address (manyToMany). A user wants to add a person with the same address, that has a previous person.
Person Address Address
+------------------------+ +-------------------------------+
| person_id | address_id | | id | postCode | city |... |
+------------------------+ +-------------------------------+
| 1 | 1 | | 1 | 15800 | New York |... |
| 2 | 1 | | 2 | 25385 | London |... |
+------------------------+ +-------------------------------+
Person
+------------------+
| id | name | ... |
+------------------+
| 1 | Jack | ... |
| 2 | Peter | ... |
+------------------+
Is there some automatic way to:
Check if the same address already exists. If does add connection instead of create a duplicate address.
When Update Jack (from the example) and his address, add a new address instead of updating also address of Peter.
When delete check if there is an address connected with another person. If not delete an address, otherwise delete only connection.
In this case it's quite easy to check before changes are persisted to DB, but in more complex example it's really annoying when you have to check 3 records in the same moment that are all in addition connected.

Resolving complex foreign keys in QSqlRelationalTable

I'm working with QSqlRelationalModel and I have some problem.
For example if we deal with simple tables like:
Location
+------+--------+
| id | name |
+------+--------+
Department
+------+-------------+
| id | location_id |
+------+-------------+
Then I can write:
departmentModel = new QSqlRelationalTableModel(this);
departmentModel->setTable("Department");
departmentModel->setRelation(Department_LocationId, QSqlRelation("Location", "id", "name"));
departmentView = new QTableView;
departmentView->setModel(departmentModel);
departmentView->setItemDelegate(new QSqlRelationalDelegate(this));
and it'll work fine, and display location names instead of ID's.
But in my case I can't apply this approach. Suppose I have next tables:
Person
+------+-------------+
| id | firstname |
+------+-------------+
Experience
+------+------------------+
| id | person_id (FK) |
+------+------------------+
Participant
+------+-----------------+
| id | experience_id |
+------+-----------------+
Assume that I want to use Participant as QSqlRelationalTable:
QSqlRelationalTable participantModel;
participantModel->setTable(Participant);
...
participantView->setModel(participantModel);
participantView->setItemDelegate(new QSqlRelationalDelegate(this));
And I want to display Person.firstname instead of experience_id in view (and also I don't want lose editing funcionality). How can I do this?
I can't use setRelation() as in example above, because:
participantModel->setRelation("experience_id", QSqlRelation("Experience", "id", WHAT_DO_I_HAVE_TO_WRITE_HERE_TO_GET_WHAT_I_WANT);
I can't write "person_id", because I want to have firstname displayed instead of person_id.
I think you should redesign your tables into something like this
Participant
+------+------------------+----------------------+
| id | person_id (FK) | experience_id (FK) |
+------+------------------+----------------------+
Person
+------+-------------+
| id | firstname |
+------+-------------+
Experience
+------+--------------+--------
| id | experience | year . . . (and so on)
+------+--------------+-------------
And now you can:
departmentMode->setTable("Participant");
departmentModel->setRelation(1,QSqlRelation("Person","id","firstName"));
departmentModel->setRelation(2,QSqlRelation("Experience","id","experience"));
I am quite confused on how you really want it to be applied in your project because of how you made your tables. Maybe if you can describe what your plan is so I can help more.

Resources