I have 2 tables:
sma_db
+----+----------+-------+
| ID | title | catid |
+----+----------+-------+
| 1 | Hi | 4 |
| 2 | Hello | 4 |
| 3 | Test | 5 |
+----+----------+-------+
sma_files
+----+----------+---------+
| ID | name |entry_id |
+----+----------+---------+
| 1 | a.jpg | 1 |
| 2 | b.jpg | 1 |
| 3 | c.jpg | 2 |
+----+----------+---------+
My query as:
$sql = mysql_query("SELECT * FROM sma_db
LEFT OUTER JOIN sma_files
ON sma_db.id = sma_files.entry_id
WHERE catid = '4'") or die(mysql_error());;
while($affcom = mysql_fetch_assoc($sql)){
$title = $affcom['title'];
$name = $affcom['name'];
echo $title;
echo $name;
}
How to select only one row from sma_files table ???
The output for above query:
Hi a.jpg
Hi b.jpg
Hello c.jpg
The output I need:
Hi a.jpg
Hello c.jpg
$sql = mysql_query("SELECT * FROM sma_db
LEFT OUTER JOIN sma_files
ON sma_db.id = sma_files.entry_id
WHERE catid = '4'
GROUP BY sma_files.entry_id")
or die(mysql_error());
try below query
SELECT * FROM `sma_db`
INNER JOIN `sma_files`
ON `sma_db`.`id` = `sma_files`.`entry_id`
WHERE `sma_db`.`catid` = '4'"
Use Group By
SELECT * FROM sma_db
LEFT OUTER JOIN sma_files
ON sma_db.id = sma_files.entry_id
WHERE catid = '4'
GROUP BY `entry_id`
Related
I'm needing to retrieve shared values from a table based on a value from another table, but don't show duplicates.
Example of what tables I have...
Table - members
+-----------------+
| ID | NAME |
+-----------------+
| 1 | Bob |
| 2 | Jack |
| 3 | Jane |
| 4 | Bruce |
| 5 | Clark |
| 6 | Peter |
+-----------------+
Table - groups
+--------------------------------+
| ID | NAME | MANAGER_ID |
+--------------------------------+
| 1 | Group A | 1 | (Bob)
| 2 | Group B | 2 | (Jack)
| 3 | Group C | 1 | (Bob)
+--------------------------------+
Table - group_members
+--------------------------------+
| ID | GROUP_ID | MEMBER_ID |
+--------------------------------+
| 1 | 1 | 3 | (Group A - Jane)
| 2 | 1 | 4 | (Group A - Bruce)
| 3 | 1 | 5 | (Group A - Clark)
| 4 | 1 | 6 | (Group A - Peter)
| 5 | 2 | 3 | (Group B - Jane)
| 6 | 3 | 4 | (Group B - Bruce)
| 7 | 3 | 5 | (Group C - Clark)
+--------------------------------+
What I am needing
(Note: I'm using * in queries here to shorten code.)
If 'Bob' sees all his groups.
Look at 'group_members' table and show all members that belong to it...
$q = SELECT * FROM groups WHERE manager_id = $id;
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch-assoc($r) {
$q2 = SELECT *, count(MEMBERS_ID) AS group_count FROM group_members LEFT JOIN members ON group_members.MEMBER_ID = members.id WHERE group_id = '$row[GROUP_ID]';
$r2 = mysqli_query($dbc, $q2);
while ($row2 = mysqli_fetch-assoc($r2) {
echo $row2['name'];
}
}
This shows me the list as expected.
+------------------------+
| NAME | GROUP COUNT |
+------------------------+
| Jane | 1 |
| Bruce | 1 |
| Clark | 1 |
| Peter | 1 |
| Bruce | 1 |
| Clark | 1 |
+------------------------+
I Add GROUP BY group_members.group_id to my 2nd query and that shows.
+------------------------+
| NAME | GROUP COUNT |
+------------------------+
| Jane | 1 |
| Bruce | 2 |
| Clark | 2 |
| Peter | 1 |
+------------------------+
Which is perfect... But here is the problem
if I add a WHERE members.name LIKE \'%clark%\' then it outputs...
+------------------------+
| NAME | GROUP COUNT |
+------------------------+
| | |
| | |
| Clark | 1 |
| | |
| | |
| Clark | 1 |
+------------------------+
It ignores GROUP BY and shows blank rows where other entries would show.
So with all that said. Does any one know why or a better way to do this please?
Been at it for a while now and would really appreciate any assistance.
EDITED:
Here's the full query with all the columns used:
$q = SELECT * FROM groups WHERE manager_id = $id;
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch-assoc($r) {
$q2 = SELECT members.id) AS memid, members.first, members.last, members.comname, members.email, members.sector, (members.status) AS memstatus, (group_members.id) AS groupid, (group_members.member_id) AS memidgroup, group_members.group_id, COUNT(group_members.member_id) AS groupcount, member_roles.role FROM members LEFT JOIN group_members ON members.id = group_members.member_id LEFT JOIN member_roles ON members.role_id = member_roles.id WHERE group_id = '$row[GROUP_ID]' AND members.name LIKE '%clark%' GROUP BY group_members.group_id;
$r2 = mysqli_query($dbc, $q2);
while ($row2 = mysqli_fetch-assoc($r2) {
echo $row2['name'];
}
}
Your query or problem is not completely stated. One cannot guess or assume.
Post your last query as well as all queries dont worry about saving the space.
Those blank rows have data that why they are in the table.
Base on your explanation or your query, here is my simple answer
SELECT id,
(select groups.id from groups where groups.id = group_members.group_id )AS group_members_id,
(select groups.name from groups where groups.id = group_members.group_id )AS group_members_name,
(select members.id from members where members.id = group_members.member_id )AS members_id,
(select members.name from members where members.id = group_members.member_id )AS members_name,
count((select members.id from members where members.id = group_members.member_id ) )as members_id_count FROM group_members WHERE (select members.name from members where members.id = group_members.member_id ) LIKE '%clark%' group by members_id
As you mentioned
WHERE members.name LIKE \'%clark%\'
you were selecting data from the members table whereas you have to select the data from group_members table.
Hello I have two tables to send consecutive queries.
For example, table A yields 1,2,3 ..
Then, look for data in table B of query 1,2,3 ..
tableA
_____________________
| uid | rate |
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 4 |
tableB
_________________________
| rate | text |
| 1 | ONE |
| 2 | TWO |
| 3 | THREE |
| 4 | FOUR |
===
<?php
$sql = $con->query("SELECT * FROM tableA WHERE uid=1");
$user = $sql->fetch_array();
$ratings = $user['rate']; //1,2,3
$sql2 = $con->query("SELECT * FROM tableB WHERE rate IN('".$ratings."')");
$text = $sql2->fetch_array();
$results = $text['text']; //ONE, TWO, THREE
?>
How best to do that?
You can use this query:
Select tableA.*,tableB.*
from tableA
join tableB on tableA.rate=tableB.rate
where tableA.uid=1
I have this table:
// mytable
+----+---------+
| id | related |
+----+---------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 1 |
| 5 | 3 |
| 6 | 2 |
| 7 | 4 |
| 8 | 4 |
| 9 | 2 |
+----+---------+
I have just an id number like $id = 6. And I want to select all rows which have identical related. So this is expected result:
// newmytable
+----+---------+
| id | related |
+----+---------+
| 3 | 2 |
| 6 | 2 |
| 9 | 2 |
+----+---------+
How can I do that?
Here is my query but doesn't work:
SELECT *
FROM mytable m
WHERE (SELECT related
FROM mytable
WHERE id = :id) t m.related = t.related
select m2.id, m2.related
from
mytable m1
join mytable m2
on m2.related = m1.related
where m1.id = 6
SQL Fiddle sample
SELECT * FROM mytable
WHERE related = ( SELECT related FROM mytable WHERE id = 6)
Not tested...
Try this
select *from mytable
where related = (select related from mytable where id = 6)
Hello please replace your query with below query you will get your expected output.
SELECT * FROM mytable WHERE = (SELECT related FROM mytable WHERE id = :id)
This will work
SELECT * FROM mytable WHERE related = (SELECT releated FROM mytable WHERE id = 6)
I have 3 table : events,students and parent_student_relation.
events
id | class_id | title
----------------------
1 | 16 | ABC
2 | 17 | JKL
3 | 18 | XYZ
students
id | class_id | name
----------------------
5 | 18 | StudentName1
6 | 17 | StudentName2
7 | 16 | StudentName3
parent_student_relation
student_id | parent_id
----------------------
4 | 10
5 | 10
6 | 11
7 | 11
Now i want to get data from events where i pass parent_id = 10 then get following output.
Output
id | class_id | title
----------------------
3 | 18 | XYZ
And i pass parent_id = 11 then get following output.
Output
id | class_id | title
----------------------
1 | 16 | ABC
2 | 17 | JKL
I tried with following code :
$q_student = "SELECT student_id FROM parent_student_relation WHERE parent_id = " . $parent_id;
$q_class = "SELECT class_id FROM students WHERE id IN($q_student) GROUP BY class_id";
$q = "SELECT * FROM events WHERE class_id IN($q_class)";
So i can get perfect output in $query = mysql_query($q);
But I want to all in only one join query. So how can do it?? Or its possible or not?
try this
$query="SELECT *
FROM parent_student_relation AS a
JOIN students AS b
ON a.student_id = b.id
JOIN events AS c
ON b.class_id = c.class_id
WHERE a.parent_id = " . $parent_id;
Use this:
$query="select e.id,e.class_id,e.title,s.id from event e
inner join student s on e.class_id=s.class_id
inner join parent_student_relation p on s.id=p.student_id
where p.parent_id=$parent_id";
Now I would like to query data to do the newest get code.
Now I have 2 table,
this is t1.
+--------------+--------------------------+----------------------+
| id | name | description |
+--------------+--------------------------+----------------------+
| 1 | GG | GG is good |
| 2 | ABC DEFG | ABC DDDDD |
| 3 | CCARD | Gooooo |
+--------------+--------------------------+----------------------+
this is t2
+---------+------------+-------------------+------------------+
| id | kaid | code | timestamp |
+---------+------------+-------------------+------------------+
| 1 | 2 | ZZZZAAAAA | 123456789 |
| 2 | 2 | AAAZZADWWW | 123344444 |
| 3 | 1 | ASFASDFFFF | 123333333 |
| 4 | 2 | HHHHHFDFG | 123222222 |
| 5 | 1 | ASDASDADDDD | 123111111 |
+---------+------------+-------------------+------------------+
I want the data show like this:
ORDER BY timestamp desc limit 5
+--------+------------+------------------+------------------
| id | kaid | name | time |
+--------+------------+------------------+------------------+
| 1 | 1 | GG | 123111111 |
| 2 | 2 | ABC DEFG | 123222222 |
| 3 | 1 | GG | 123333333 |
| 4 | 2 | ABC DEFG | 123344444 |
| 5 | 2 | ABC DEFG | 123456789 |
+--------+------------+------------------+------------------+
now my code is:
$querylist = mysql_query("SELECT * FROM t1 ORDER BY time desc limit 5");
while($rowlist = mysql_fetch_row($querylist)) {
$idlist[] = $rowlist['id'];
$user_list_latest[] = $rowlist;
}
how do I select the t2.name when t1.id = t2.kaid?
thank you very much!
SELECT t1.*,t2.name FROM t1,t2 where t1.id = t2.kaid ORDER BY t1.time desc limit 5
you can use like this
You need to use join like as
select t1.name, t2.* from t2
join t1 on t2.kaid = t1.id
order by time desc limit 5
You have to join the tables like this, and like Jay said stop using mysql_* functions:
$querylist = mysql_query("SELECT t1.*, t2.name FROM t1 INNER JOIN t2 ON t1.id=t2.kaid ORDER BY t1.time desc limit 5");
while($rowlist = mysql_fetch_row($querylist)) {
$idlist[] = $rowlist['id'];
$user_list_latest[] = $rowlist;
}
Try this
$querylist = mysql_query("SELECT t2.*, t1.name FROM t2
INNER JOIN t1
ON t1.id=t2.kaid
ORDER BY t2.time desc
LIMIT 5");