How to not exist on another table on this two table?
Users table
User_relationships table
and this is my current query
"SELECT * FROM `users`
WHERE CONCAT(first_name, " ", last_name) LIKE '%' . $name . '%'
AND (`role_id` = 6 OR `role_id` = 4)
ORDER BY `first_name` asc limit 15"
and I want to add on query where guardian_id(user_id) not exist on the user_relationships table
UPDATE 1
select * from `users`
where not exists
(select 1 from `user_relationships`
inner join `users` on `user_relationships`.`guardian_id` = `users`.`id`
where `user_relationships`.`student_id` = 422)
I tried this and still returns me zero result.
I only have var name = ? and student_id = ?
try to use "NOT IN" query function with 'DISTINCT'.
following may help you.
select * from users where CONCAT(first_name, " ", last_name) LIKE '%' . $name . '%' and (role_id= 6 or role_id= 4) and (id NOT IN (select DISTINCT guardian_id from User_relationships where student_id = $student_id)) order by first_name ASC limit 15
Let me know if you still need some changes.
I search related title using this function :
$related = Access::FETCH("SELECT title, MATCH(title) AGAINST('".$DB_QUERY[0]['title']."') AS score
FROM " . SONGS . "
WHERE MATCH(title) AGAINST('".$DB_QUERY[0]['title']."')
ORDER BY score DESC LIMIT 5");
foreach($related as $row){
echo $row['title'];
}
ie:
//search title clapton
$DB_QUERY[0]['title'] = 'clapton';
//result
clapton // same title
eric clapton
clapton song
clapton guitar
This worked for me But in result search and print same title + other title. how do can i find related title without show same title?!
you can use
"SELECT title, MATCH(title) AGAINST('".$DB_QUERY[0]['title']."') AS score
FROM " . SONGS . "
WHERE MATCH(title) AGAINST('".$DB_QUERY[0]['title']."')
and title <> '".$DB_QUERY[0]['title']."'
ORDER BY score DESC LIMIT 5"
Hi I am trying to retrieve my topics from table topics but in my look up list I want the most recent active topic to be showing, all the comments are in different table. How could I do it that my list shows all the topics from the topics table but puts them in order according to the most recent date from the comments table?
$forums = mysql_query("select * from forumtopic where category='$who' order by date DESC LIMIT " . (($page - 1) * 10) . ", 10");
while($forum = mysql_fetch_array($forums))
{
so I want it to be like
$forums = mysql_query("select * from forumtopic where category='$who' and select from forumcomment where category='$who' but order by date from forumcomment DESC LIMIT " . (($page - 1) * 10) . ", 10");
while($forum = mysql_fetch_array($forums))
{
if that makes sense
I believe this is what you wanted to do:
SELECT * FROM forumtopic
WHERE category='$who'
ORDER BY FIELD( topic,
(SELECT topic
FROM forumcomment
WHERE category='$who'
ORDER BY date DESC))
LIMIT " . (($page - 1) * 10) . ", 10
The order by field lets you specify an order for the table forumtopic, which is ordered by most active, also I don't know the schema of your table so you have to change the names accordingly
EDIT - SIMPLER SOLUTION, JOIN THEN ORDER BY
SELECT forumtopic.topic FROM forumtopic
INNER JOIN forumcomment
ON forumtopic.topic = forumcomment.topic
WHERE category = '$who'
ORDER BY forumcomment.date DESC
LIMIT " . (($page - 1) * 10) . ", 10
I have a table that displays books. However I would like only only 1 book to be shown per unique email address (strContactEmail). I tried the below query, it didn't work. Any help greatly appreciated.`
$sql = "SELECT lngbookid, strTitle, strAuthor, strcoursecode, strISBN, ".
" strcontactname, DISTINCT(strContactEmail) AS strContactEmail, ".
" curPrice, ysnsalerent, dtmpostdate, memcomments, school, ".
" ASIN, BookIMG, ISBN10, ISBN13, Updated, ".
" datetime, user_ip, NoOtherBooks ".
" FROM tblbooks ".
" WHERE strcoursecode LIKE '%$search%' ".
" ORDER BY datetime DESC LIMIT 50";
Try a GROUP BY statement:
http://www.w3schools.com/sql/sql_groupby.asp
The easiest way:
SELECT max(lngbookid) as lngbookid,
max(strtitle) as strtitle,
max(strauthor) as strauthor,
max(strcoursecode) as strcoursecode,
max(strisbn) as strisbn,
max(strcontactname) as strcontactname,
strcontactemail,
max(curprice) as curprice,
max(ysnsalerent) as ysnsalerent,
max(dtmpostdate) as dtmpostdate,
max(memcomments) as memcomments,
max(school) as school,
max(asin) as asin,
max(bookimg) as bookimg,
max(isbn10) as isbn10,
max(isbn13) as isbn13,
max(updated) as updated,
max(datetime) as datetime,
max(user_ip) as user_ip,
max(nootherbooks) as nootherbooks
FROM tblbooks
WHERE strcoursecode LIKE '%$search%'
GROUP BY strcontactemail
ORDER BY datetime DESC
LIMIT 50
EDIT
Well, the above was actually too "dummy". Better way to do this is (providing that column "lngbookid" is a primary key):
SELECT a.lngbookid,
a.strtitle,
a.strauthor,
a.strcoursecode,
a.strisbn,
a.strcontactname,
a.strcontactemail,
a.ontactemail,
a.curprice,
a.ysnsalerent,
a.dtmpostdate,
a.memcomments,
a.school,
a.asin,
a.bookimg,
a.isbn10,
a.isbn13,
a.updated,
a.datetime,
a.user_ip,
a.nootherbooks
FROM tblbooks AS a
JOIN (SELECT strcontactemail,
Max(lngbookid) AS lngbookid
FROM tblbooks
GROUP BY strcontactemail) AS b
ON ( a.strcontactemail = b.strcontactemail
AND a.lngbookid = b.lngbookid )
I am using a constant NEWS_POST_NUMBER and i am getting confused on how to attach it to a string to query it to database. i tried many things and it is giving errors.
here is the string i tried.
$query = "
SELECT news.id, news.timestamp, news.title
FROM news
ORDER BY id DESC
LIMIT $from, NEWS_POST_NUMBER";
please note NEWS_POST_NUMBER is the constant i have defined i want to attach it to query how do i do it?
$query = "SELECT news.id, news.timestamp, news.title FROM news ORDER BY id DESC LIMIT $from, " . NEWS_POST_NUMBER;
Use the concatenation operator ;)
$query = "
SELECT news.id, news.timestamp, news.title
FROM news
ORDER BY id DESC
LIMIT $from, " . NEWS_POST_NUMBER;