I am doing a foreach loop and selecting each row that equals that section name. However, I need to also show all results that don't equal any of the section names. Below is the part of my code that is selecting rows where it equals the section name.
$result2 = mysqli_query($con, "SELECT * FROM sections ORDER BY `order`");
$sectionnames = array();
while($row = mysqli_fetch_array($result2)) {
$sectionnames[] = $row['sectionname'];
}
foreach ($sectionnames as $sectionname) {
$result = mysqli_query($con,"SELECT * FROM faq WHERE section = '$sectionname' ORDER BY `order`");
Do you mean something like this:
SELECT * FROM `faq` WHERE `section` NOT IN (SELECT `sectionname` FROM `sections`)
which would select all faq items that do not have a section that shares a name with any sectionname in the sections table? If not then perhaps what juergen d posted is what you need. It's a little unclear exactly what your end goal is, so feel free to clarify and I will update.
I think it should be,
$result = mysqli_query ($con, "SELECT * FROM faq WHERE section NOT IN
(SELECT sectionname FROM sections)");
You can use a left join to get the data in 1 query
SELECT *
FROM sections s
left join faq f on f.section = s.sectionname
ORDER BY s.`order`, f.`order`
SELECT section FROM Faq
LEFT JOIN Sections
ON Faq.section = Sections.sectionname
WHERE Sections.sectionname IS NULL
Related
I'm having issues with this code, it's displaying multiple of the same table headings and I don't know why.
Heres the code :
#This section of code will display my affected_countries table
echo"<br><br>This will list and display important information about the statistics of the infections<br>";
$sql = "SELECT * from affected_countries,infection_info";
$result = mysqli_query($db, $sql);
echo"<table border='1'><tbody>";
echo"<tr><td><b>Name</b></td><td><b>Country Origin</b></td><td><b>Number of Countries affected</b></td></tr>";
while($row = mysqli_fetch_assoc($result)) {
echo"<tr><td>{$row['NAME']}</td><td>{$row['Country Origin']}</td><td>{$row['Number of Countries affected']}</td></tr>";
}
echo"</tbody></table><br>";
Output on website:
Two Tables above
Table below(Issue)
It just has multiple of the same thing and I don't know what I should do
Any help is appreciated
You have to use SQL joins to get columns from multiple tables.
See the example query:
SELECT ac.name, ii.country_origin, ii.number_of_countries_affected FROM affected_countries ac INNER JOIN infection_info ON ac.name =ii.name;
You should list from two tables separately
$sqlAffected = "SELECT * from affected_countries";
$sqlinfection_info = "SELECT * from infection_info";
$resultA = mysqli_query($db, $sqlAffected);
$resultB = mysqli_query($db, $sqlinfection_info);
or something like this:
SELECT * FROM affected_countries UNION infection_info;
or
SELECT * FROM affected_countries LEFT JOIN infection_info on afftected_countries.primary_key = infection_info.affected_country_id;
Basically, I am seeking to know if there is a better way to accomplish this specific task.
Basically, what happens is I query the db for a list of "project needs" -- These are each uniquer and only appear once.
Then, I search another table to find out how many members have the required "skills - which are directly correlated to the project needs".
I accomplished exactly what I was trying to do by running a second query and then inserting them into an array like this:
function countEachSkill(){
$return = array();
$query = "SELECT DISTINCT SKILL_ID, SKILL_NAME FROM PROJECT_NEEDS";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result)){
$query = "SELECT COUNT(*) as COUNT FROM MEMBER_SKILLS WHERE SKILL_ID = '".$row['NEED_ID']."'";
$cResult = mysql_query($query);
$cRow = mysql_fetch_assoc($cResult);
$return[$row['SKILL_ID']]['Count'] = $cRow['COUNT'];
$return[$row['SKILL_ID']]['Name'] = $row['SKILL_NAME'];
}
arsort($return);
return $return;
}
But I feel like there has to be a better way (perhaps using some kind of join?) that would return this in a result set to avoid using the array.
Thanks in advance.
PS. I know mysql_ is depreciated. It is not my choice on which to use.
SELECT P.SKILL_ID, P.SKILL_NAME, COUNT(M.SKILL_ID) as COUNT FROM PROJECT_NEEDS P INNER JOIN MEMBER_SKILLS M
ON P.SKILL_ID=M.SKILL_ID
GROUP BY P.SKILL_ID, P.SKILL_NAME
I've adjusted Nriddens answer to accomodate for the select distinct, Im under the belief that his adjustment would be ok given SKILL_ID is a primary key
function countEachSkill(){
$return = array();
$query = "
SELECT
COUNT(*) AS COUNT,
PROJECT_NEEDS.SKILL_NAME,
PROJECT_NEEDS.SKILL_ID
FROM
(SELECT DISTINCT
SKILL_ID, SKILL_NAME
FROM
PROJECT_NEEDS) AS PROJECT_NEEDS
INNER JOIN
MEMBER_SKILLS
ON
MEMBER_SKILLS.SKILL_ID = PROJECT_NEEDS.SKILL_ID
GROUP BY PROJECT_NEEDS.SKILL_ID";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result)){
$return[$row['SKILL_ID']]['Count'] = $row['COUNT'];
$return[$row['SKILL_ID']]['Name'] = $row['SKILL_NAME'];
}
arsort($return);
return $return;
I am subquerying on the select distinct because I dont believe you have a dedicated skills table with an auto inc primary key, if that was there I wouldn't be using a subquery.
Can you test this query
select project_needs.*,count(members_skills.*) as count from project_needs
inner join members_skills
on members_skills.skill_id=project_needs.skill_id Group by project_needs.skill_name, project_needs.skill_id
I first search all questions info. from "question" table including title, content, user etc.
the Code:
$sql = "select * FROM question where id>0 ORDER BY id ASC";
$result1 = mysql_query($sql);
$res=Array();
And then I want to search the user's point from "user" table. So I must search point for each user in each row from the result1
The Code:
while($rows=mysql_fetch_assoc($result1))
{
$res[]=$rows;
$user = $rows['user'];
$sql2 = "select point from user where name='$user'";
$result2 = mysql_query($sql2);
}
My problem is how to combine all the users' point(result2) with the questions info.(result1) together so that I can return a json for each row.
Use left join, as my understanding this work for you
$sql = "SELECT q.*, u.point AS point FROM question AS q LEFT JOIN user AS u ON q.user = u.name WHERE q.id > 0 ORDER BY q.id ASC";
$result = mysql_query($sql);
It's better go with the joins here i am giving you the query.i hope it may helps you
select * from question q,user u where q.id>0 ORDER BY id ASC
try something like this:using left join
select question.*,user.point FROM question left join user on user.name= question.name where id>0 ORDER BY id ASC
i have a online application for wich i require a sort of dashboard (to use the white-space).
There are three tables used for the operation:
1.) categories: id, name
2.) entries: id, name, description, category_id, created, modified
3.) entryimages: id, filename, description, entry_id
on the dashboard i want to show 4-5 entries (with thumbnail images, so i require joins to the entryimages table and the categories table) for each category.
I read through some articles (and threads on s.o.) like this one:
http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/
But am still not getting it right, i've tried to first extract all categories and for each and every category build a query and with "all union" attach them to one, but that is not working.
The last version of code i used:
foreach($categories as $id => $name)
{
$query .= "SELECT `entry`.`id`,
`entry`.`name`,
`entry`.`description`,
`entry`.`category_id`,
`entry`.`created`,
`entry`.`modified`,
`entryimages`.`filename`,
`entryimages`.`description`
FROM `entries` as `entry` LEFT JOIN `entryimages` ON `entryimages`.`entry_id` = `entry`.`id`
WHERE `entry`.`category_id` = $id ";
if($i < count($groups))
{
$query .= 'UNION ALL ';
}
$i++;
}
$result = mysql_query($query);
Does anybody know what is the best right to accomplish this operation?
Thanks 1000
On the dashboard if you want to show three entries, the way you are doing is wrong. If my understanding is right, the entire query will be something like
"SELECT `entry`.`id`,
`entry`.`name`,
`entry`.`description`,
`entry`.`category_id`,
`entry`.`created`,
`entry`.`modified`,
`entryimages`.`filename`,
`entryimages`.`description`
FROM `entries` as `entry`
INNER JOIN categories
ON (entry.category_id = categories.id)
LEFT JOIN (SELECT * FROM `entryimages` WHERE `entry_id` = `entry`.`id` LIMIT 1) AS `entryimages`
ON `entryimages`.`entry_id` =`entry`.`id`
ORDER BY `entry`.`created` DESC LIMIT 5";
Your code looks ok to me you should just add a LIMIT clause so that you get just five of them and an ORDER BY clause to get the latest
$query .= "SELECT `entry`.`id`,
`entry`.`name`,
`entry`.`description`,
`entry`.`category_id`,
`entry`.`created`,
`entry`.`modified`,
`entryimages`.`filename`,
`entryimages`.`description`
FROM `entries` as `entry` LEFT JOIN `entryimages` ON `entryimages`.`entry_id` = `entry`.`id`
WHERE `entry`.`category_id` = $id ORDER BY `entry`.`created` DESC LIMIT 5";
I read an article and its comments from mysql database with two separate queries as
$result = mysql_query("SELECT * FROM articles WHERE article_id='$id'");
$row = mysql_fetch_array($result);
$title=$row['title'];
........
AND
$result = mysql_query("SELECT * FROM comments WHERE article_id='$id'");
while($row = mysql_fetch_array($result)) {
$comment_title=$row['title'];
.........
}
Is the best way to read this set of data from database? OR
Is it possible to catch the data through one query or one transaction?
NOTE: My issue is that first query for article is only for one row; but the second one needs a loop to process (and display in html) several comments.
$result = mysql_query("SELECT articles.title as article_title, comments.title as comment_title FROM articles LEFT JOIN comments ON articles.article_id = comments.article_id WHERE articles.article_id = '$id'");
$row = mysql_fetch_array($result);
$article_title=$row['article_title'];
$comment_title=$row['comment_title '];
You can do it in this way -
SELECT a.*, c.* FROM articles a
LEFT JOIN comments c
ON a.article_id = c.article_id
WHERE a.article_id='$id';
Is the best way to read this set of data from database?
Sure, it is.
Is it possible to catch the data through one query
Yes, it's possible but there is no point in doing that.
Why do you ask?