I want to write a query to search in multiple tables (news, articles, projects, files)
While searching I found this
SELECT title, post FROM news WHERE MATCH (title,post) AGAINST ('press');
I tested it and it's working, but i failed to extend this to multiple tables.
How to write one query that return me search results for multiple tables ?
One method is to use a union:
SELECT title, post FROM news WHERE MATCH (title,post) AGAINST ('press')
UNION
SELECT title, post FROM articles WHERE MATCH (title,post) AGAINST ('press')
UNION
SELECT title, post FROM projects WHERE MATCH (title,post) AGAINST ('press')
UNION
SELECT title, post FROM files WHERE MATCH (title,post) AGAINST ('press')
This now essentially becomes one pseudo table with all of the records merging into one dataset.
This is one way to do it. I'm not sure what Match does, but in the where you can also your Match function
Select Table1.Title, Table2.Post FROM Table1, Table2 WHERE Table1.Title = 'press' AND Table2.Title = 'press'
This query will give you the Title and the Post of the 2 tables that have both have press in it.
Have a look on this query , you may use like this -
SELECT *
FROM news t1, articles t2, projects t3, files t4 WHERE MATCH ( t1.title, t1.post)
AGAINST ('press') or Match(t2.title,t2.post) AGAINST('press')
and set all column you want to search set in MATCH() function.
You could try this, it may helpful for you.
Related
I have to display result with select multiple column but distinct on only one column i have also used following queries and also refereed below link.
Query One.
select category , offer_id, store_image from `tbl_coupan_offer` where
`offer_id` in (Select max(`offer_id`) FROM `tbl_coupan_offer` group by `category`)
Above queries return all record with including duplicate
I want only display distinct category not repeated
Following is image
On image you can see Travel and Accessorises repeated
Second query
SELECT DISTINCT `category`,`offer_id`,`store_image` FROM `tbl_coupan_offer`
I also refereed this link Select all columns from rows distinct on one column
Remove offer_id from the select:
SELECT DISTINCT category
FROM tbl_coupon_offer;
If you want one offer_id, then use GROUP BY:
SELECT category, MAX(offer_id)
FROM tbl_coupon_offer
GROUP BY category;
Like Gordon said but as a subquery to get the appropriate image.
SELECT DISTINCT q.category, ch_offer_id, store_img
FROM
(
SELECT category, MAX(offer_id) ch_offer_id
FROM tbl_coupan_offer
GROUP BY category
) q
JOIN tbl_coupan_offer
ON q.ch_offer_id=tbl_coupan_offer.offer_id
SELECT category , MAX(`offer_id`) , store_image
FROM `tbl_coupan_offer`
GROUP BY `category`
If multiple data coming then just add DISTINCT after SELECT
Check your database engine type. It is "InnoDB" or not. If your database and table both existed and if you find this error #1146 Table xxx doesn't exist. Then read more about database engine.
If all is right, then query of Gordon is right.
SELECT `category`, `offer_id`, `store_image`
FROM `tbl_coupon_offer`
GROUP BY `category`
We need to grab the last and newest 20 entries from different tables. However, the GROUP BY statement skips records because we are working with LEFT JOIN on tables.
All these records are linked to unique persons in another table. We store these person's id's in an array for more queries later.
We have a few tables (in which all those person id's are stored) and we want to get them sorted and grouped.
The tables are like this:
SELECT lastRecord+personID FROM t1
SELECT lastRecord+personID FROM t2
SELECT lastRecord+personID FROM t3
SELECT lastRecord+personID FROM t4
WHERE t5.Essential_Column_Name = '1'
GROUP BY personID
ORDER BY 'all the latest entries'
LIMIT 20
With that, the relevance of all the latest entries should be equal.
We do have a timestamp column as well. Perhaps that might work better.
Any input is highly appreciated!
For people looking for an answer on this; this is the right post, answer and update to this Q:
UNION mysql gives weird numbered results
With thanks to all for the ideas and providing the paths to the right solution.
I have a mysql table(table1) which has the following row:
topic_id: 1
topics: programming
description: A programming language is an artificial language designed to...
I have another table(table2) with this row:
desc_id: 1
description: In mathematics and computer science, an algorithm is an effective...
topics: mathematics, computer science, programming
What I'm looking to do is to run a query to compare the two topics fields and let me know which topics exist in table2 that don't exist in table1.
For instance, comparing the two above I'd like to run a query to let me know that topics mathematics and computer science don't exist in table1.
I would use a subquery, but it can also be done with innerjoins :
SELECT *
FROM `table2`
WHERE `topics` NOT IN (
SELECT DISTINCT(topics)
FROM `table1`
)
you can try NOT IN
i.e.
SELECT topics FROM table2 where topics NOT IN( select topics from table1)
If you normalized your table2 so that the topics list is in a separate sub-table, this would be a trivial query. As it stands now, it's difficult as by default mysql won't see those seperate topics in table2.topics as discrete topics. It's just a long string that happens to have commas in there.
Thankfully, MySQL has the find_in_set() function, which can help out immensely, but this function isn't available elsewhere. Not having access to your dataset, I'm just guessing here, but this should do the trick:
SELECT table1.topics, count(table1.topic_id) AS cnt
FROM table1
LEFT JOIN table2.topics ON FIND_IN_SET(table1.topics, table2.topics) = 0
GROUP BY table1.topics
HAVING cnt = 0
Basically, join the tables wherever the table1 topic is NOT in a table2 topic and count how many times the table1 topic shows up like this. If it shows up zero times, then it's present in at least one record in table2.
normalize by creating a third table, one that links table 2 to table 1 with a many to many relationship.
Table_1
id, etc
Table_2
id, etc
Table_3
id, table1_id, table2_id
you could then use simple joins to create a query that will pull the relavent data
SELECT * FROM Table_1 LEFT JOIN Table_3 ON Table_1.id = Table_3.table1_id WHERE Table_3.table2_id = $table2_id
This will pull all topics for the course.
i have a pictures table : pictures(articleid,pictureurl)
And an articles table : articles(id,title,category)
So, briefly, every article has a picture, and i link pictures with article using articleid column. now i want to select 5 pictures of articles in politic category.
i think that can be done using IN but i can't figure out how to do it.
Note: Please only one query, because i can do it by selecting articles firstly then getting the pictures.
Thanks
To get five pictures from articles in a category you could do this:
SELECT pictures.pictureurl
FROM articles, pictures
WHERE articles.id = pictures.articleid AND articles.category = 'politic'
ORDER BY [your sort criteria]
LIMIT 5;
You could consider rephrasing the question a bit.
If you are looking for an IN query instead of a JOIN this is an alternative to Alex's query:
SELECT pictureurl
FROM pictures
WHERE arcticleid IN (SELECT id FROM articles WHERE category='politic')
LIMIT 5
Rewritten for clarification (see comments):
If you like to keep your JOIN criteria separated from your SELECT criteria, you can write something like the below:
SELECT pictureurl
FROM pictures
JOIN articles ON id = articleid
WHERE category LIKE 'politics'
ORDER BY RAND()
LIMIT 5
I find the intent slightly clear when it's written like that, and maybe it's just me, but I have encountered complex queries written in the SELECT * FROM a, b, c form that worked under MySQL 4 which choke MySQL 5, whereas the above format works fine with both.
Also, if you use uniform ID column names for conformity, and to avoid confusing yourself in more complex scenarios, you can use the USING clause instead. So if articles ID column is also named articlesid, the JOIN could be written like so:
SELECT pictureurl
FROM pictures
JOIN articles USING (articleid)
...
You don't really need to use IN for this. IN serves when you nest queries or when you have a known set of values to check against.
To select 5 random images in the politics category:
SELECT pictureurl FROM articles, pictures WHERE pictures.articleid = articles.id AND articles.category = 'politics' ORDER BY RAND() LIMIT 5
Hey stackoverflow - This is my first question here.
I have 2 tables in my mySQLdb.
[tab_artist]
|id|artist|count
[tab_songtitle]
|id|songtitle|artistId
Im trying to select from both tab_artist.artist and tab_songtitle.songtitle as suggest where searchclause = m
I have tried this
SELECT artist, songtitle AS suggest
FROM tab_artist, tab_songtitle
WHERE artist LIKE('m%') OR songtitle LIKE('m%')
But this gives me 2 columns, artist and suggest
if the search is met i need artist to give me e.g. metallica.. but only once - but in songtitles i need all titles starting with met.
Hope this makes sence to the right expert :)
A union should do it:
select artist AS suggest from tab_artist WHERE artist LIKE 'm%'
union all
select songtitle AS suggest from tab_songtitle WHERE songtitle LIKE 'm%'
For this case, I would use union all so you won't get duplicates removed, but you may want this if, for example, Metallica has a self-titled album and you only want the word to appear once. In that case, union would be more appropriate.
You need a join:
Select artist, songtitle from tab_artist inner join tab_songtitle on tab_songtitle.artistID = tab_artist.ID where artist LIKe ('m%') OR songtitle like ('m%')
you want to use SQL UNION
select id, artist, count FROM table1
UNION
select id, title, artistid from table2
It basically concats the two result sets vertically. There are some restrictions and modifications, but generally thats all you need to do.
Use a UNION ALL to concatenate the results from the artist and song tables.
SELECT 'Artist' AS type, id, artist AS suggest
FROM tab_artist
WHERE artist LIKE 'm%'
UNION ALL
SELECT 'Song', id, songtitle
FROM tab_songtitle
WHERE songtitle LIKE 'm%'
Don't use UNION as some are suggesting as that will do a completely unnecessary (and expensive) DISTINCT on the query results.