Hopefully someone has done this so has a bit of code which may help me
I currently have this query
ForumID = request("ID")
QID = ForumID
DB.CreateQueryDef QID, "SELECT * FROM Topic WHERE Keywords = '" & QID & "' ORDER BY LastPost DESC"
What I have is a table topic which contains the topics. I have added a field keywords, which will have keywords eg "motocross, mx, motox"
How can i query the database eg any table which contains the keyword motocross?
Also how can this use multiple words
eg
two topics
topic1 -> Keywords motocross, mx
topic2 -> Keywords motocross, dance
if eg search.asp?ID= motocross unrelated
it will search database for both, eg get all topics where keywords field contain eith of the 2 words in query, or 4 words or more
Also is it possible to do relavent search like youtubes like this?
two topics
topic1 -> Keywords motocross, 1
topic2 -> Keywords motocross, 2
eg search.asp?ID=motocross 1
topic 1 will show first as both words in query match "motocross" and "1" whereas topic2 only has motocross
is it possible to do this so it will count keyword matches
have you tried using LIKE ??
"select * from table where field LIKE '%" & value & "%'"
First of all, your querystring should not contain spaces. Use a different delimiter. A + is a good one to use. So your URL will look like: search.asp?ID=motocross+1 instead.
This will split your kewwords into an array, then take them one at a time to build the and clause. The replace removes the first AND.
SQL = "SELECT * FROM Topic WHERE"
ForumID = request("ID")
QID = Split(ForumID, "+")
For Each KW in QID
SQL = SQL & "AND Keywords Like '*" & QID & "*' "
Next
SQL = Replace(SQl, "WHEREAND", "WHERE")
SQL = SQL & " ORDER BY LastPost DESC"
you need to make your Query with "OR" ....
I have 2 keyword(car , Bus) so my query will be like
DB.CreateQueryDef QID, "SELECT * FROM Topic WHERE (Keywords = 'car' OR Keywords = 'Bus') ORDER BY LastPost DESC"
this is just example as static query you can make it run time with loop of your comma separated values..
Related
I am working on converting a legacy MS access system to a spring-boot application and I came across a big query. I am mostly done with converting the functions from access to mysql but not able to understand how to convert the following DLookUp sub-query as a mySql subquery
DLookUp("[price]","tbl_so","[so_id] = " & tbl_trade.so_id & " AND [product_id] = " & tbl_po.product_id
What I understood is following won't work as I don't have the Inner Joins set between the 3 tables, tbl_so, tbl_po, tbl_trade
SELECT tbl_so.price FROM tbl_so WHERE tbl_so.so_id = tbl_trade.so_id AND tbl_so.product_id = tbl_po.product_id
My question is how do I know how the tables will join with each other in this case and also when this DLookUp query is seldom used. And also the solution for this query.
Well, as a general rule, dlookup() can be replaced with a left join. However, you can also use a sub-query and they tend to be "less" change to the query.
So, if we had this:
SELECT id, partNum, dlookup("PartDescrt","tblParts","PartID = " & partNum)
as Description from tblOrders.
You would replace the above dlookup() with a sub-query like this:
SELECT id, partNum,
(select PartDescrt from tblParts where tblParts.PartID = tblOrders.PartNum)
AS Description
from tblOrders
The above is SQL or access sql syntax, but quite sure the same should work for MySQL.
If there is more then one partNumber that matches the above, then add a TOP 1, and a order by with some unique row (PK ID column is best). So the query becomes:
(select TOP 1 PartDescrt from tblParts where tblParts.PartID = tblOrders.PartNum
ORDER BY PartDescrt, ID DESC)
AS Description
How can i get values from mysql database usin while loop?
Sry for my bad english and i'm new in vb.net so please do not insult me because I do not know vb.net, I'm trying to learn, i'll try to explain what i need. :)
Lets go:
We know that EmailAddress is real email address, now first query must be something like this
Query1 = "select ID from database.table_users where email='" & EmailAddress & "'"
Now we know that Query1 will return value id from table_users.
Now we need create new query like this
Query2 = "select friend_id from database.table_friends where user_id='" & query1 & "'"
Okay, now Query2 return friend_id from table_friends.
One more query like this
Query3 = "select displayname from database.table_users where id='" & Query2 & "'"
Query3 must return displaynames of all users which we got in query2.
Example:
First i need query ID from table_users where email=EmailAddres
Then i need query friend_id from table_friends where user_id=ID
After that i nedd query displayname from table_users where id=friend_id
*these are just examples and they are not correct as you can see :D
I need 3 querys in while loop, and only last query (displayname) will go to the listbox. While must return all mysql values from table_friends where ID= id of EmailAddress user.
Join your tables into a single query. Something like this:
SELECT
Friends.displayname
FROM
table_users AS Users
INNER JOIN table_friends AS Connections
ON Users.user_id = Connections.user_id
INNER JOIN table_users AS Friends
ON Connections.friend_id = Friends.user_id
WHERE
Users.email = #EmailAddress
This creates two references to the table_users table, one for the initial user and one for the friends, connected by your many-to-many linking table. (Note also the use of a query parameter, which I highly recommend instead of directly concatenating input into the query.)
SQL is very powerful for querying structured data (hence its name), you'll be much better off building your query logic into SQL rather than keeping the SQL queries overly simple and making multiple trips to the database.
why loop and why can't you make it single query like below using JOIN
select tf.displayname from table_users tu
join table_friends tf on tu.id = tf.friend_id
and tu.email = '" & EmailAddress & "';
I'm trying to return records for an alert system based on two conditions.
The first condition is there is a booking in the system for tomorrow's date [Date()+1] with a Type value of B. If that JobNumber also has a Type value (in another record) of A AND the Result field's value is "Not Approved" we need to return an alert.
Example table:
JobNumber Type Company Date Result
58129 B 3 22/03/2013
58129 A 3 20/03/2013 Not Approved
58129 C 3
So far I have been able to create a SQL query in VBA to return the results of the first condition and have looped through the results to return the relevant JobNumbers. How do I insert these JobNumbers as criteria for the second SQL query or is it possible to combine all criteria into one SQL statement?
My SQL so far:
strSQL1 = "SELECT I.JobNumber, I.Type, I.Company, I.Date, I.Result " & _
"FROM tblInspection I " & _
"WHERE (((I.Type)='B') AND ((I.Date)=Date()+1));"
strSQL2 = "SELECT I.JobNumber, I.Type, I.Company, I.Date, I.Result " & _
"FROM tblInspection I " & _
"WHERE (((I.Type)='A') AND ((I.Result)<>'approved'));"
Any help would be much appreciated.
You can get a field from the same or another table. This will give an error if more than one row is returned, but whether or not more than one row will be returned depends on your data. If it is likely, you will need to add another criterion, such as date.
SELECT I.JobNumber, I.Type, I.Company, I.Date, I.Result,
(SELECT Result
FROM tblInspection q
WHERE q.JobNumber=i.JobNumber
AND Result="Not Approved"
AND Type="A")
As ResultA
FROM tblInspection I
WHERE I.Type='B' AND I.Date=Date()+1
for example: the table named products there is a product name field in the table. i want to show some related items which matched by the knowed products's product name.
how to write the sql query command?
eg: if the knowed products's product name is
True Blood Season 1 DVD
i want to get all the product name which begins as True Blood Season..
if the knowed products's product name is
24 Hours Season 7 DVD
i want to get all the product name which begins as 24 Hours Season..
the sql: what's error with the sql
$query ="select p.products_id, pd.products_name, p.products_price
from " . TABLE_PRODUCTS . " p " .
TABLE_PRODUCTS_DESCRIPTION . " pd
where p.products_status = 1
and p.products_id = pd.products_id
and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
and p.products_id <> :product_id AND MATCH('products_name') AGAINST (:products_name IN NATURAL LANGUAGE MODE);
I'd look at using MySQL Full-Text search.
This should be able to take the title of your chosen product and use it to retrieve relevant matches, eg
SELECT * FROM `products`
WHERE `id` <> :product_id -- don't return current product
AND MATCH(`product_name`)
AGAINST (:product_name IN NATURAL LANGUAGE MODE)
This requires you to create a full-text index on your products.product_name column.
select * from products where name like 'True Blood Season%'
select * from products where name like '24 Hours Season%'
SELECT * FROM products WHERE product_name='title of show here';
It's pretty basic SQL, I'd really recommend reading up a bit before posting as this question is very entry level. I'm more than happy to help you get started though :).
I am just trying to translate a section of a query:
...OR... AND Zip.id IN (SELECT plan_id FROM plans_zips
WHERE zip_id = (SELECT id FROM zips WHERE title = '" . $Zip . "'))
From what I can tell the query is saying this:
Get all Zip.ids (from zips table) and get plan_id (from plan_zips table)
WHERE zip_id (using plans_zips) = zip.id where the full zip (title) matches the var $Zip.
what you're suggesting is right.
For MySQL it's much more optimal to use joins in place of nested sub-queries like this. The optimizer will be unable to optimize sub-queries and they must be run with the deepest one first.
Not quite.
If you indent the SQL a bit, it will become clearer.
...OR... AND
Zip.id IN
(SELECT plan_id FROM plans_zips WHERE zip_id = //Get all the plan_ids where
(SELECT id FROM zips WHERE title = '" . $Zip . "'))//the zip_id is the value returned from this query.
I agree with James C though - joins are much better and easier to read