Online computer courses, code, programming tutorial and sidebar information for monitoring Canadian S&P/TSX index. Build friendship and networking. Welcome to visit my blogs often!!! I also have two other sites: YouTube Channel and Google site.
Adsense
Popular Posts
- PHPWind-- A PHP forum script applcaition in China
- How to blend adsense inside your post?
- Formatting my post
- Notepad++ - Add C++ compiler
- Install PHPMailer 5.2.4 and use smtp gmail
- Wayback Machine - see archived versions of web pages across time
- Set up a child account and set screen time limit in Windows 8
- phpexcel toggle expand and hide column in EXCEL and summary
- Install PHP ibm_db2 extension in Linux (redHat)
- PHP: add a download as pdf file button in report page
Thursday, March 7, 2013
MySQL: select one table from another table
Suppose MySQL tables edu_courses and edu_extra1_study have a smae primary key: course_id, to select from edu_courses based on the condition in edu_extra1_study, we can use following MySQL statement if requiring order by title:
SELECT * FROM edu_courses WHERE course_id In (SELECT course_id FROM edu_extra1_study) ORDER BY title;
If edu_extra1_study selection is based on the variable in url
data_export.php?c1=1&c2=0&c3=0
where c1, c2 and c3 from checkbox, i.e, the value equal to if the checkbox is checked.
We we defined an arry in PHP:
$q1= Array();
If the checkbox is checked., we can add a condition, such as:
$q1[]='competency=3';
finally we can use PHP implode to join all the conditions:
implode(' AND ',$q1);
Final code:
<?php
$querystring='SELECT course_id FROM edu_extra1_study ';
$q1= Array();
if(isset($_GET['c1'])){
if($_GET['c1']==1) { $q1[]='competency=3';}
}
if(isset($_GET['c2'])){
if($_GET['c2']==1) {$q1[]='competency=2';}
}
if(isset($_GET['c3'])){
if($_GET['c3']==1) {$q1[]='cognitive=1';}
}
if(!empty($q1))$querystring=$querystring.'WHERE '.implode(' AND ',$q1);
if(empty($q1))
{$sql = "SELECT * FROM edu_courses ORDER BY title";}
else
{$sql = "SELECT * FROM edu_courses WHERE course_id in (".$querystring.")ORDER BY title";}
?>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment