Get Name From Another Table MySQL - php

I have two table in my database called
number_list and number_status
I am currently getting perfect result using below query
SELECT * FROM number_status
WHERE number = '".$_SESSION['number1']."' OR
number = '".$_SESSION['number2']."'
ORDER BY id DESC LIMIT $start, $limit
Now I want name from table number_list. Both table have number is common. I have tried some Left Join etc but I am learning MySQL yet so not getting proper result. Let me know if someone can help me for do it.
Thanks

You can use a join query as follows
SELECT s.id, s.number, l.name FROM number_list l JOIN number_status s ON l.number=s.number WHERE s.number = '".$_SESSION['number1']."' OR s.number = '".$_SESSION['number2']."' ORDER BY l.id DESC LIMIT $start, $limit
or you can directly get the name as you have the number in hand as follows
SELECT name FROM number_list WHERE number = '".$_SESSION['number1']."' OR number = '".$_SESSION['number2']."' ORDER BY id DESC LIMIT $start, $limit

Related

Order a table by another table column?

I have this query:
$q = "SELECT * FROM user
WHERE sec='1' AND reg_by='".$_SESSION['login_username']."'
ORDER BY date DESC LIMIT $startrow, 30 ";
I have another table which stores appointments, it has a column named meet.
How can I sort this query by meet?
Not all data at users are in other table.
You can use the below query. Replace another_table with your original table name :
$q = "SELECT u.* FROM user AS u LEFT JOIN another_table AS at ON u.userid = at.userid WHERE u.sec='1' AND u.reg_by='".$_SESSION['login_username']."' ORDER BY at.meet DESC LIMIT $startrow, 30 ";
you can use joining for this like
select user.*,meet.* from user left join meet on (meet.userid = user.id) where user.sec='1' AND user.reg_by='".$_SESSION['login_username']."' order by meet.userid DESC
$q = "SELECT * FROM user INNER JOIN user
ON meets.userid=user.userid WHERE sec='1' AND reg_by='".$_SESSION['login_username']."' ORDER BY date DESC LIMIT $startrow, 30 ";

MySQL Order by another tables data

I'm having difficulty understanding how to Order a query by data from another table.
The existing query is: SELECT ID FROM UserTour WHERE Live = 1 ORDER BY LastUpdated DESC
This obviously Orders by the column 'LastUpdated' in the table 'UserTour'
However, I need it to be ordered by the column 'LastUpdated' which is in another table 'ImageLinks', Where 'TypeID' = 16 (again in 'ImageLinks').
I hope that makes sense.
So it would be something like: $ids = #mysql_values('SELECT ID FROM UserTour WHERE Live = 1 ORDER BY ('Select ID FROM 'ImageLinks' Where TypeID = 16 Order by LastUpdated DESC')');
Any help would be appreciated on how to do this. Cheers
If there is no relationship between the two tables your query in your question will look like this
select id from
(
SELECT
ID
, (Select ID FROM ImageLinks Where TypeID = 16
Order by LastUpdated DESC limit 1) as order_val
FROM UserTour
WHERE Live = 1
) x
ORDER BY x.order_val
which will work but will not do not any ordering as the order_val column will have a fixed value.
If the IDs are linked 1:1 (no indication that they are, but just supposin') we could do this:
select u.id
from UserTour u inner join ImageLinks i on u.ID = i.ID
where u.Live = 1 and i.TypeID = 16
order by i.LastUpdated desc
If the above is incorrect then you will have to decide how the two tables are related and join them correspondingly.
In other words, If the tables are in no way connected, then you cannot provide an ordering of one table's data based on a column in the other.
UPDATE
select
i.LinkID
, i.LastUpdated
from UserTour u inner join ImageLinks i
on u.ID = i.LinkID
where u.Live = 1 and i.TypeID = 16
group by i.LinkID, i.LastUpdated
order by i.LastUpdated desc LIMIT 30

how can i grab the last value of a coloumn

I have three tables
1.course(c_id(pk),c_name,sem_no);
2.student(s_id(pk),s_name,user_name,password);
3.student_info(s_id(fk),c_id(fk));
I have logged in the student, THEN i run this query
SELECT distinct sem_no FROM course,student_info WHERE course.c_id=student_info.c_id and s_id='0001' ORDER BY sem_no ;
It shows all the semester he passed including the running semester..
now
I want to show the last value of sem_no column as his current semester..
how can I grab the last value of the sem_no?
Any help will be appreciated!
Here is an improved version of what you want:
SELECT sem_no
FROM course c join
student_info si
on c.c_id = si.c_id and s_id = '0001'
ORDER BY sem_no desc
LIMIT 1;
Note the use of proper ANSI join syntax. Also notice the use of table aliases, which make the query easier to read.
Try this:
SELECT sem_no
FROM course as t1 join
student_info as t2
on t1.c_id = t2.c_id
WHERE t2.s_id = '0001'
ORDER BY sem_no desc
LIMIT 1;
When selecting from mysql, you can order by either ascending or descending. ascending would be from 1-10 eg. and descending would be 10-1. So
SELECT distinct sem_no FROM course.student_info
WHERE course.c_id=student_info.c_id and s_id='0001' ORDER BY sem_no desc LIMIT 1;
would return last row, and only last row.

sub queries order by

I am trying get records from question order by sub query (qcat) table. and my code is
"SELECT * FROM question
where survey_name='$_SESSION[ssn_sname]' AND
qcategory IN
( SELECT qcategory FROM qcat
WHERE client_name='$_SESSION[ssn_sname]'
GROUP BY qcategory
ORDER BY p_order
) AND
status='1' AND
survey_name LIKE'%$sname%
LIMIT $start, $limit";
But it did not get results in order.
how can i get rows order by the qcat table?
In the sub query you do not need the GROUP BY clause - This is used for aggregated functions
You also do not need the ORDER BY in the sub query - Let mysql work it out for the in bit
Add the ORDER BY for the whole query. i.e. at the end
So the SQL should be
"SELECT * FROM question
where survey_name='$_SESSION[ssn_sname]' AND
qcategory IN
( SELECT qcategory FROM qcat
WHERE client_name='$_SESSION[ssn_sname]'
) AND
status='1' AND
survey_name LIKE'%$sname%
ORDER BY p_order
LIMIT $start, $limit";
I also think that you may be able to avoid the sub query in the first place. But that would require a little thinking and a bit more knowledge about the tables.
BTW - Note the possibility of SQL injection
You need to join with the qcat table in order to be able to sort on a different column within that table. Try this:
$query = "
SELECT q.id question_id, q.*, c.*
FROM question q
INNER JOIN qcat c ON c.category = q.category
WHERE q.survey_name='$_SESSION[ssn_sname]'
AND c.client_name = '$_SESSION[ssn_sname]'
AND q.status='1'
AND q.survey_name like '%$sname%'
GROUP BY q.id
ORDER BY c.p_order
LIMIT $start, $limit";
Note: Your query is vulnerable to SQL Injection!

PHP/MySQL Query In a loop, how to append to just one query?

I have a query to pull all articles out of the database..
$get_articles = $db->query("
SELECT *, a.id AS id, s.type AS type FROM ".$prefix."articles a
LEFT JOIN ".$prefix."sources s ON (s.id = source_id)
WHERE a.type!='trashed' ORDER BY a.timestamp DESC LIMIT $start, $end");
Within the loop of this query, I do then do another query on the same table to find related articles to the 'title' of the article, stored as '$related_phrase'. The query within the loop is:
// get related articles to this entry
$get_related = $db->query("
SELECT *, a.id AS id, MATCH (title, description) AGAINST ('$related_phrase') AS score FROM ".$prefix."articles a
LEFT JOIN ".$prefix."sources s ON (s.id = source_id) WHERE a.type!='trashed' AND MATCH (title, description) AGAINST ('$related_phrase') AND a.id!='$articles[id]' HAVING score > 7
ORDER BY a.timestamp DESC LIMIT 0, 3");
This basically means we have a query in a loop which is causing the pages to load very slowly.
What we want to do, is bring the query from within the loop, in the main query, so it's all working within one query, if that's possible?
Any help very much appreciated!
I don't think you would gain much speed by merging the two queries.
One thing you could try is to get a list of all articles and DISTINCT searchphrases (in e.g. temptable), and then build a query to get all related articles in one single go. Lastly match up related articles with the article list.
try this:
$articles_and_related = $db->query("
SELECT *
FROM ".$prefix."articles art
LEFT JOIN (
SELECT * FROM ".$prefix."articles x
WHERE
score > 7
AND x.type != 'trashed'
AND x.id != art.id
AND MATCH(x.title, x.description) AGAINST (art.title)
LIMIT 3
) rel
LEFT JOIN ".$prefix."sources s2 ON (s2.id = rel.source_id)
LEFT JOIN ".$prefix."sources s ON (s.id = art.source_id)
WHERE
art.type!='trashed'
ORDER BY art.timestamp DESC LIMIT $start, $end");

Categories