i have two table for article and setting. in article table i put id,title,date ..etc and in settings table, i have row with name featuredarticle and put id of article with separate comma for featured articles. now i need to list/print my featured article only. how to ?
TABLE articles:
ID | TITLE | DATE | PUBLISH
1
2
3
4
5
TABLE settings / row featuredarticle:
1,2,5
SELECT a.*
FROM articles a
INNER JOIN settings b
ON FIND_IN_SET(a.ID, b.featuredarticle) <> 0
FIND_IN_SET
If you have time to change the table schema, change it. It is a bad design to have columns vith value separated by a comma.
Get data from DB to array and use:
$newArray = explode(".", $dataDbArray);
You can try this-
SELECT * FROM article
JOIN settings ON FIND_IN_SET( id, featuredarticle)
Please have a look at demo: demo
Related
I have two table suppose products and auto_assign_prod_list. I want to populate a dropdown list with the id of products table that are not present in auto_assign_prod_list table.
Suppose,
product table contain
Id
------
1
2
3
4
5
auto_assign_prod_list table contain
Id
-----
1
5
So, my result set will be
2
3
4
How is it possible using MySQL and PHP ?
Try this:
SELECT Id FROM product
WHERE Id NOT IN (SELECT Id FROM auto_assign_prod_list)
It will select the ids from product table which are not in auto_assign_prod_list table.
Result:
Id
------
2
3
4
See result in SQL Fiddle.
use a left join
select p.id
from products p
left join auto_assign_prod_list a on a.id = p.id
where a.id is null
SQLFiddle demo
See this great explanation of joins
I made some researchs but couldnt find yet the exact concept needed to achieve this:
::: EDIT ::: my initial request wasnt clear so here I try again
I have two tables,
table_01 with four columns: id / name / address / id_cat
AND
table_02 with two columns: id_cat / category
id_cat in table_01 and table_02 is just an INT(10), while category is a VARCHAR supposed to contain the exact name of the category (which is quite long). What I am trying to do is, when a query is made on table_01, the echo shows rows of table_01 as result, but instead of showing id_cat as a number, it shows the text from table_02 category.
The result shows as I am expecting it, but, if for example in table_02, I have a rowid_cat = 1 , category = AAAAA, I would like to echo the id_cat from table_01 as the category from table_02, so still for example, 002 | standon | 125 market street | AAAAA instead of what I get now, which is 002 | standon | 125 market street | 1.
I didnt create relation between both tables yet.
If I understand what you are after, your query for the data would just need to be something like:
select t1.id as id, t2.category as category from tab_01 as t1
left join tab_02 as t2 on t1.id_cat = t2.id_cat where t1.id = '5';
The 5 is obviously the id you are looking for in tab_01.
I am not fully clear about your question. But I think this will help you.
Right now you have created one-to-many relationship between this two tables. This means one category (tab_02) can have multiple tab_01 data.
You can try this query:
SELECT t1.id_cat AS id_cat_number, t2.*
FROM tab_02 t2
INNER JOIN tab_01 t1 ON t1.id_cat = t2.id_cat
WHERE t2.category = XXXXXXXX
I'm making a list with certain songs. Each song has its own unique ID. OK let's say I have this table called list. A new:
The ID is self-explanatory. It's used to ID rows. song_one through song_ten is filled with a song's unique ID.
Every time a user makes a new list, they add songs and each row gets filled. Now, how would I get the average rank of the songs in the tables list using the song's ID that is filled between song_one through song_ten?
Redesign your database. Make a new table with songid, listid and rank. This will make your job easy.
listsongs
-----------------
songid [PK] -- Foreign key referencing the songs table
listid [PK] -- Foreign key referencing the lists table
rank
Selecting average song ranks:
SELECT
a.song_id
AVG(b.rank) AS avgrank
FROM
songs a
LEFT JOIN
listsongs b PM a.song_id = b.song_id
GROUP BY
a.song_id
Please do as others have said about normalizing your DB structure. If you wish to continue with this design however, you can use this clunky-looking solution:
SELECT
a.song_id,
AVG(
CASE
WHEN a.song_id = b.song_one THEN 1
WHEN a.song_id = b.song_two THEN 2
WHEN a.song_id = b.song_three THEN 3
WHEN a.song_id = b.song_four THEN 4
WHEN a.song_id = b.song_five THEN 5
WHEN a.song_id = b.song_six THEN 6
WHEN a.song_id = b.song_seven THEN 7
WHEN a.song_id = b.song_eight THEN 8
WHEN a.song_id = b.song_nine THEN 9
WHEN a.song_id = b.song_ten THEN 10
END
) AS AvgRank
FROM
songs a
INNER JOIN
list b ON
a.song_id IN
(
b.song_one,
b.song_two,
b.song_three,
b.song_four,
b.song_five,
b.song_six,
b.song_seven,
b.song_eight,
b.song_nine,
b.song_ten
)
GROUP BY
a.song_id
I would listen to #Mark Byers and #Shehzad Bilal, who said that you need to redesign your database structure.
When you think in the terms of tables and their attributes, think logical - think in the terms of code.
For example: If you are writing to a file, would it be easier to create a universal loop to output all the things needed, or to open the file with different pieces of code every time you needed to write something.
In your database, it would be easier to have one table that represents the song itself (that is the general idea behind a database design) than having a table that represents all the songs.
(table) (attribute)
song
id
albumid (fk from table album)
name
title
(...etc)
list
id
songid (fk from table song)
ip
date
(...etc)
If you wanted to create a ranking system, you would do it through code. In some cases, purely depending on your design, you would have a table for it, but it would also be universal.
I am having a table named tb_search_report which contains the fields search_report_id,sales_id,cat_name,search_keyword. My purpose is to create a list in the admin side of my website to get the list of the keywords used to search in the front end as well as the count of those keywords.
For that i used the following query:-
SELECT search_keyword,
cat_name,
COUNT(search_keyword) AS cnt
FROM tb_search_report GROUP BY search_keyword
The result is as follows,
search_keyword |cat_name | cnt
------------------------------------
NULL |Handbags | 6
Shoes | | 1
Fabrics | | 3
Now i need to get/list the top search results viewed in the front end along with the above table in another column. The search listing comes from tbl_sales whose primary key is sales_id. How can i display the names of the sales sales_title in top search sales from search keyword. Need help. Thanks in advance.
You can join the tables so that you can show the sales_title
SELECT tbl_sales.sales_title,tb_search_report.search_keyword,tb_search_report.cat_name,
COUNT(tb_search_report.search_keyword) AS cnt
FROM tb_search_report
INNER JOIN tbl_sales ON tb_search_report.sales_id = tbl_sales.sales_id
GROUP BY search_keyword
Just update the above query that will suit your requirements
There's a table that has three fields: id, word, username
And it currently has data like this:
1 | run | thisguy
2 | run | thisguy
3 | go | thatgirl
4 | go | thatguy
5 | go | thatgirl
The ones I want is 1, 3, and 4. I don't want 2, and 5 because they're duplicates.
How would I take them out?
Doing on the same table :
Create a unique index on the required fields/
Doing in new table :
a) Create a temporary table by selecting unique data from original table.
b) Drop original table.
c) rename temporary table to original table's name.
CREATE TABLE tempr SELECT id, type,name FROM myTable GROUP BY type,name;
DROP TABLE myTable;
ALTER TABLE tempr RENAME TO myTable ;
Also create unique index on type,name on this table to prevent future duplicates.
DELETE t2 FROM mytable AS t1
JOIN mytable AS t2 USING (word, username)
WHERE t1.id < t2.id;
You can use mysqls build in function distinct or GROUP BY
Distinct
Mysql_query("SELECT DISTINCT id, word, username FROM persons");
Group by
Mysql_query("SELECT * FROM persons GROUP BY word");
of course you can group by username or whatever you would like too.
Jonas
if you want to have only unique records in your table then do what DhruvPathak has said in his answer. But if you want to get the records through query then you can use distinct keyword. Your query will look like this
select id,distinct(word),username from $table
Have you tried using DISTINCT clause?