i have a database: database with a table: table and some fields:
id fname dphone count_pic dup_id
6055903 Karla 5126xxx798 1 57
6173767 Aaliyah 4082xxx534 4 39
5611411 Aaliyah 4082xxx534 15 39
5611211 Aaliyah 4082xxx534 18 39
4234798 Abby 3057xxx974 31 16
6166691 Walter 6178xxx280 1 74
3375576 Walter 6178xxx280 17 74
what i am trying to do is to select the fields that have the smallest count_pic and the ones that have the bigger count_pic and that have the same dup_id
any ideas how to do this in mysql?
thanks.
With this query you'll select the smallest and the biggest values of count_pic for every dup_id
SELECT MIN(count_pic) AS minpic,
MAX(count_pic) AS maxpic,
dup_id
FROM `table`
GROUP BY dup_id
If you also need corresponding rows, then you could use something like
SELECT *
FROM `table` t1
INNER JOIN (SELECT MIN(count_pic) AS minpic,
MAX(count_pic) AS maxpic,
dup_id
FROM `table`
GROUP BY dup_id) t2 ON t1.dup_id = t2.dup_id
AND (t1.count_pic = minpic
OR t1.count_pic = maxpic)
Related
Edit (for clarification): Hannes wants to search records with a certain VID and associated records from the same table with the condition AID (from original records) = TID.
EDIT, because wrog problem description (sorry for that!)
I need a query in MySQL that loops through the selected rows and creates a result array. The pattern for this is shown below.
Currently I call a select again and again. Is there a way to create a single query from this? Thanks a lot!
Table structure
ID, VID, AID, ... (title, etc.) ... TID
Select over the vid field
SELECT id, vid, aid FROM tbl WHERE vid = 24;
ID VID AID
1 24 NULL
2 24 NULL
3 24 712
4 24 713
5 24 714
6 24 715
7 24 716
...
If aid not null => select the same table with the AID value as condition for another field
SELECT * FROM tbl WHERE TID = 112
SELECT * FROM tbl WHERE TID = 113
SELECT * FROM tbl WHERE TID = 114
SELECT * FROM tbl WHERE TID = 115
SELECT * FROM tbl WHERE TID = 116
Result should be (all null rows and the selected rows from above)
ID VID AID ...
1 24 NULL
2 24 NULL
75 24 712
79 24 713
88 24 714
92 24 715
97 24 716
The problem in this e.g. is that i need a loop and tones of selects. Pseudocode:
rows = []
foreach (main_select)
{
if (aid != null)
{
selected_row = select * from tbl where tid = aid
rows[] = selected_row
}
else
{
rows[] = main_select_row_from_iteration
}
}
You should be able to do all of that with a single query
SELECT tbl1.ID as TBL1_ID, tbl1.VID as TBL1_VID, tbl1.AID as TBL1_AID,
tbl2.ID as TBL2_ID, tbl2.VID as TBL2_VID, tbl2.AID as TBL2_AID
FROM tbl AS tbl1
LEFT JOIN tbl AS tbl2 ON tbl1.AID = tbl2.TID
WHERE tbl1.VID = 24 AND tbl1.AID IS NOT NULL
It's a self-join of the same table, the WHERE condition makes sure that you have the initial VID and that there's not NULL value in AID. The JOIN takes AID and matches it to TID.
The fields in the SELECT list are from both, separated via alias.
I have four tables
1.student-it contains id name etc
2.marks - it contains markid,student_id,batch,class,section,subject_id,exam_id,obt_mark
3.subject table - it contains sub_id,sub_name
4. exam_type- examtype_id, exam_name
i need mark ledger for batch-2014,class=5,section=1,exam_id=3 as shown below-
NAME ROLL NO SUB1 SUB2 SUB3 TOTAL RESULT POSITION RANK
RAM 0054 65 54 64 183 PASS FIRST 2
HAri 0054 65 54 65 184 PASS FIRST 1
gopal 0054 65 50 65 180 PASS FIRST 3
saroj 0054 65 44 65 174 PASS FIRST 4
Thanks in advance .
Here i have sql query
SELECT `marklists`.`mrk_sub_id1`, `marklists`.`mrk_marks`, `marklists`.`mrk_practical`,
`marklists`.`mrk_exam_type`, `students`.`id` as st_id, `students`.`st_roll`,
`students`.`st_name`, `students`.`batch`, `students`.`st_class`,
`students`.`st_section`, `courses`.`id`, `classes`.`class_name` as class_name,
`courses`.`sb_name` as subject_name, `courses`.`sb_fullmark` as full_marks,
`courses`.`sb_passmark` as pass_marks FROM (`marklists`)
LEFT JOIN `classes` ON `marklists`.`mrk_class` = `classes`.`id`
LEFT JOIN `students` ON `marklists`.`mrk_student_id`=`students`.`id`
LEFT JOIN `courses` ON `marklists`.`mrk_sub_id`=`courses`.`id`
WHERE `marklists`.`mrk_exam_type` = '3' AND `marklists`.`mrk_batch` = '3'
AND `marklists`.`mrk_class` = '1' AND `marklists`.`mrk_section` = '1'
and I need ledger like above any help??
Join the tables based on common key and for the titles which you need as subject you can use pivot operator to convert rows to column and use where clause for rest of the conditions
hope this helps.
I have a scenario that a query who will work like select records based on status = 1 with highly number of records found in database.
suppose i want to select artists who have max number of status = 1 with their names fetch from other table.
another problem is my query is repetiting records.
my query:
SELECT order.status,
order.artist_id,
web_user.uFname,
web_user.uLname,
web_user.uID
FROM `order`
INNER JOIN `web_user`
ON web_user.uID = order.artist_id
HAVING order.status = '1'
my record set i am getting:
status artistid uFname uLaname uID
1 14 Pitbull Armando 14
1 14 Pitbull Armando 14
1 13 Enrique Iglesias 13
1 15 Robyn Rihanna 15
1 15 Robyn Rihanna 15
1 15 Robyn Rihanna 15
i simple want:
downloadtimes artistid uFname uLaname uID
2 14 Pitbull Armando 14
1 13 Enrique Iglesias 13
3 15 Robyn Rihanna 15
please enhanced my query.
You can use COUNT()
SELECT
COUNT(*) AS downloadtimes ,
order.artist_id,
web_user.uFname,
web_user.uLname,
web_user.uID
FROM `order`
INNER JOIN `web_user` ON web_user.uID = order.artist_id
WHERE order.status = '1'
GROUP BY order.artist_id
you should use the count
like:
COUNT(*) AS downloadtimes
you should just add the like you full code:
SELECT
COUNT(*) AS downloadtimes ,
order.artist_id,
web_user.uFname,
web_user.uLname,
web_user.uID
FROM `order`
INNER JOIN `web_user` ON web_user.uID = order.artist_id
WHERE order.status = '1'
GROUP BY order.artist_id
and refer this following link:
SQL COUNT
All id are auto_increment. I will try explain my issue understandable. I just want copy 2 tables to another 2 tables. Here my tables at below:
table1
id number
10 100
11 102
12 105
13 106
table2
id number_id subnumber
52 10 10
53 11 15
54 13 40
You can see there is subnumber of some numbers. For example, WHERE id =11 from table1 has a subnumber on table2 and it is equal to 15. Now I should copy the table1 to the table : copy_table1
$sql1 = mysql_query('INSERT INTO
copy_table1 (copy_number)
SELECT
number
FROM
table1');
And the results of copy_table1 are:
copy_table1
id copy_number
100 100
101 102
102 105
103 106
Then I should copy table2 to another table copy_table2:
$sql2 = mysql_query('INSERT INTO
copy_table2 (copy_number_id, copy_subnumber)
SELECT
number_id, copy_subnumber
FROM
table2');
And the results of copy_table2 are:
id copy_number_id copy_subnumber
60 10 10
61 11 15
62 13 40
So, lets check the tables: copy_table1 and copy_table2.
You see, ID from copy_table1 is not equal and suitable to copy_number_id FROM copy_table2
And it brings me problem. How can I do that after copying tables id and copy_number_id will be suitable to each other?
$sql2=mysql_query('INSERT INTO
copy_table2 (copy_number_id, copy_subnumber)
SELECT DISTINCT
cp1.id, t2.subnumber
FROM
copy_table1 AS cp1
CROSS JOIN
table1 AS t1
USING (number)
INNER JOIN
table2 AS t2
ON
t1.id = t2.number_id ');
I want a resultset for this table:
ID Number_of_posts Number_of_user
1 100 21
2 23 34
as
ID Number_of_posts Number_of_user Number_of_posts_AND_Number_of_user
1 100 21 178
2 23 34 178
-----------------------------------------------
123 55
Is it possible to get the sum of two colums as another column/ as output in mysql?
To get cross-tab totals (horizontal and vertical):
select id,
number_of_posts as p,
number_of_users as u,
number_of_posts+number_of_users as p_and_u
from tbl
union all
select 99999 as id,
sum(number_of_posts) as p,
sum(number_of_users) as u,
sum(number_of_posts+number_of_users) as p_and_u
from tbl
order by 1
This will give you:
id p u p_and_u
----- --- --- -------
1 100 21 121
2 23 34 57
99999 123 55 178
You're complicating your query needlessly and using more memory that you have to. Pull the records in one query, then make another query to get the aggregates.
I know it doesn't answer your question, but it's what you should be doing instead. =)
SELECT id, number_of_posts, number_of_user,
(
SELECT SUM(number_of_posts + number_of_user)
FROM mytable
)
FROM mytable
SELECT SUM(Number_of_posts), SUM(Number_of_user) FROM table;
SELECT *,
(SELECT SUM(Number_of_posts) + SUM(Number_of_user) FROM TABLE) AS total
FROM table;
(Edit: Didn't originally notice it was the total total in the last column.)
Does MySQL support ROLLUP?
SELECT id,
SUM(Number_of_posts) Number_of_posts,
SUM(Number_of_user) Number_of_user,
SUM(Number_of_posts) + SUM(Number_of_user) Number_of_posts_AND_Number_of_user
FROM table
GROUP BY ROLLUP(id)
Edit: based on a quick search, in MySQL the last line might be GROUP BY id WITH ROLLUP.