fetch no of rows from table 2 with id of table 1 - mysql

I have some thing to do here with subquery but I am not able to do.
I want the result from a table with a extra field to show to no of results from other table with a column value from table 1.
table1:
CountryId Country ISO2 ISO3
table2:
id noof_country state
I have to retrive noof_country count in table 1 as count field
EDIT
my actual tables are
table 1:
ad_id job_country status delete days_left
table 2:
CountryId Country ISO2 status
I have done query in two phase:
$sql_map = "select distinct c.ISO2, c.Country, a.job_country
from rec_countries c, rec_advert a
where c.status = 1
and DATE(a.modified_date) >= CURDATE() - INTERVAL 30 DAY
and c.ISO2 <> '--'
and c.ISO2 <> ''
and c.CountryId = a.job_country
and a.status = 1
and a.`delete` = 0
and a.days_left >0
";
$res = mysql_query($sql_map);
while($row = mysql_fetch_array($res)){
$jobs_no = count($row['job_country']);
$sql_job = "SELECT COUNT( job_country ) AS jobs_no
FROM rec_advert
WHERE job_country = ".$row['job_country']."
and status = 1
and `delete` = 0
and days_left >0";
$resjob=mysql_query($sql_job);
$rowjob = mysql_fetch_array($resjob);
//here jobs_no is the count of total rows
}
Here I want to do with subquery.

If I read the question right, this should work:
SELECT
CountryId,
Country,
ISO2,
ISO3,
(
SELECT COUNT(DISTINCT noof_country)
FROM table2
WHERE table2.id = table1.CountryId
) AS noof_country_count
FROM table1
It's not immediately clear in your question which column in table1 is a foreign key to which column in table2... or if they are even related that way. If this query doesn't work for you, please clarify your schema.
Based on your updated information, try this:
select distinct c.ISO2, c.Country, a.job_country,
(
select COUNT(a2.job_country)
from rec_advert a2
where a2.job_country = a.job_country
and a2.status = 1
and a2.`delete` = 0
and a2.days_left >0
) as jobs_no
from rec_countries c, rec_advert a
where c.status = 1
and DATE(a.modified_date) >= CURDATE() - INTERVAL 30 DAY
and c.ISO2 <> '--'
and c.ISO2 <> ''
and c.CountryId = a.job_country
and a.status = 1
and a.`delete` = 0
and a.days_left >0

Related

MS SQL query with multiple search criteria across rows

I have below table and SQL query written, this query should not return any result but its returning ID = 1 , what is wrong with the SQL query? Can anyone please help?
** Note balance data type is decimal rest are varchar
ID code balance level
1 C 150.00
1 P 40027.42 F
1 P 40027.42 F
select distinct ID from table
(
(code = 'P' and balance = 40027.42 and level = 'F') or
(code = 'C' and balance = 151.00 )
)
group by ID
having count(ID) >=2
If you do not want to count the same code twice, you can use count(distinct code):
select ID
from t
where (code = 'P' and balance = 40027.42 and level = 'F')
or (code = 'C' and balance = 151.00 )
group by ID
having count(distinct code) >=2
If you want to only count a distinct set of values once, you can use a derived table/subquery to select distinct rows:
select ID
from (
select distinct id, code, balance, level
from t
) as s
where (code = 'P' and balance = 40027.42 and level = 'F')
or (code = 'C' and balance = 151.00 )
group by ID
having count(ID) >=2
rextester demo for both: http://rextester.com/LBKO57534

SQL code: adding value to one code but shown in other code as well

could somebody help me with this, please? I was checking answers around the net but still not successful.
I have two codes, code #1:
SELECT subject_note,ticket_id,created_time,status,
UPPER(SUBSTRING(datacenter,1,3)) region
FROM sort_ticket WHERE ticket_type = 1 AND status =0 AND team_type = 1 AND MONTH(FROM_UNIXTIME(closed_date)) = MONTH(NOW())
and YEAR(FROM_UNIXTIME(closed_date)) = YEAR(NOW())
AND
(
-- ASH
owner_id = 812400897
or owner_id = 1392249056
or owner_id = 739243661
or owner_id = 100002941128738
or owner_id = 619251675
or owner_id = 502392893
)
and code #2:
SELECT
subject_note,
cyborg_verify_tries,
ticket_id,
closed_date,
created_time,
status,
UPPER(SUBSTRING(datacenter,1,3)) region
FROM sort_ticket
WHERE ticket_type = 1
AND status =0
AND team_type = 1
and (FROM_UNIXTIME(closed_date)) >= DATE_SUB(now(), INTERVAL 6 MONTH)
AND
(
-- ASH
owner_id = 812400897
or owner_id = 1392249056
or owner_id = 739243661
or owner_id = 100002941128738
or owner_id = 619251675
or owner_id = 502392893
)
Both of these codes creating table and giving me the results what is good.
Problem what I have in here is I have to add manually every NEW "owner_id" into each code.
Is there any way how I could add NEW "owner_id" only into any code and second would be updated automatically? Both info are taken from the same table "sort_ticket".
Thank you for all the help.
You can use an extra table, where you put in the user id's and replace the fixed values with a select on this data:
SELECT
subject_note,
cyborg_verify_tries,
ticket_id,
closed_date,
created_time,
status,
UPPER(SUBSTRING(datacenter,1,3)) region
FROM sort_ticket
WHERE ticket_type = 1
AND status =0
AND team_type = 1
and (FROM_UNIXTIME(closed_date)) >= DATE_SUB(now(), INTERVAL 6 MONTH)
AND
(
-- ASH
select owner_id from newtable
)
same in the other select
You can use an additional table or an additonal view. Use a join to the new table/view in your queries and add new owner_id with an insert in the table or an UNION in the view.
To solve your proble with a view you can do this:
CREATE VIEW v_sort_ticket_owner AS
SELECT 812400897 as owner_id
UNION
SELECT 1392249056 as owner_id
UNION
SELECT 739243661 as owner_id
UNION
SELECT 100002941128738 as owner_id
UNION
SELECT 619251675 as owner_id
UNION
SELECT 502392893 as owner_id
--UNION
--SELECT newnumber as ownder_id
SELECT subject_note,ticket_id,created_time,status,
UPPER(SUBSTRING(datacenter,1,3)) region
FROM sort_ticket st
JOIN v_sort_ticket_owner sto
ON st.owner_id = sto.owner_id
WHERE st.ticket_type = 1 AND st.status =0 AND st.team_type = 1 AND
MONTH(FROM_UNIXTIME(st.closed_date)) = MONTH(NOW())
and YEAR(FROM_UNIXTIME(st.closed_date)) = YEAR(NOW())

Sum of two counts from different table with different conditions

Basically I need to merge these into one single query:
SELECT COUNT( DISTINCT id ) AS totalRows1
FROM other_events WHERE status = "approved"
AND Location = 1
SELECT COUNT( DISTINCT Id ) AS totalRows2
FROM core_events WHERE Status = "Active"
AND Location_id = 1
When I do it like below, if there is no event with Location_id = 1 query returns 0. In that condition I need it to return the count of the first table only.
SELECT COUNT( DISTINCT t1.id ) + COUNT( DISTINCT t2.Id ) AS total
FROM other_events AS t1, core_events AS t2
WHERE t1.status = "approved"
AND t1.Location = 1
AND t2.Location_id = 1
AND t2.Status = 'Active'
ps. column names are exactly like above
Use a UNION statement to merge the result like this:
SELECT SUM(total) FROM (
SELECT COUNT(DISTINCT id) AS total
FROM other_events
WHERE (status = "approved" AND Location = 1)
UNION ALL
SELECT COUNT(DISTINCT Id) AS total
FROM core_events
WHERE (Location_id = 1 AND Status = 'Active')
) union_result

Mysql Group by With Condition

The below data shows time schedule having elective/ non-elective subjects of a student. My requirement is to select those rows when both elec and nonelec type has same period so in this case select elec type. Means for a day's schedule elective (type elec) should be given preference when both having same period. And when type elec does not has period like period 5 then select the non-elective one.
My Query
SELECT s.sch_id, s.sch_subtype, sd.sdetail_id, sd.sdetail_period
FROM schedule s
INNER JOIN schedule_detail sd ON s.sch_id = sd.sdetail_schedule
WHERE '2014-04-30'
BETWEEN sch_datefrom
AND sch_dateto
AND
(
(
sch_section =1
AND sch_subtype = 'nonelec'
)
OR
(
sch_subtype = 'elec'
AND 272
IN
(
SELECT edetail_stuid
FROM elective_detail
WHERE edetail_elective = sch_section
)
)
)
AND sch_course =3
AND sch_batch =2
AND sch_termid =2
AND sdetail_day = 'wed'
AND sdetail_period >0
AND CASE WHEN sch_subtype = 'nonelec'
THEN 1 =1
WHEN sch_subtype = 'elec'
THEN sdetail_subject >0
AND sdetail_faculty >0
AND sdetail_room >0
END GROUP BY CASE WHEN sch_subtype = 'elec'
THEN sdetail_period
ELSE 1
END ORDER BY sdetail_period
Output of above query
Required Output
You can try something of this sort :
SELECT sch_id,sch_subtype,sdetail_id,sdetail_period
FROM table
WHERE condition
group by sch_subtype
It would be better if you can post the query which u tried.
Dont really get whats the problem here:
SELECT * FROM schedule s
JOIN schedule_detail sd ON s.sch_id = sd.sdetail_schedule
LEFT JOIN elective_detail ed ON sd.sch_section = ed.edetail_elective
WHERE
(
sd.sch_section =1
AND s.sch_subtype = 'nonelec'
)
OR
(
ed.edetail_stuid = 227
AND sch_subtype = 'elec'
)
GROUP BY sd.sdetail_period
SQL Fiddle: http://sqlfiddle.com/#!2/69d4e/1

Mysql Update + SELECT query

I want to update data table for those who score exam id 1,2 more than 80. I try this
UPDATE data
SET column = 'value'
WHERE
(SELECT * FROM exams
WHERE (id = '1' AND score >= 80) AND (id = '2' AND score >= 80));
It gives me 0 result. But it should have few hundreds results ANy help??
I think the problem is this:
SELECT * FROM exams
WHERE (id = '1' AND score >= 80) AND (id = '2' AND score >= 80)
It gives 0 result. How to select those who score more than 80 points for both exam 1 and 2??
You query won't work because you're asking for exams that have id = 1 AND id = 2.
Assuming that id cannot hold two values at the same time, you'll never return any results.
Try this as the basis of your update instead :-
SELECT * FROM exams
WHERE score >= 80 AND id IN ( '1','2' )
Edited based on comment :-
User wants only people who scored more than 80 for both exams. Assuming personid is a key to the person who took the exam.
SELECT e1.personid FROM
(
SELECT personid FROM exams WHERE score >= 80 AND id = '1'
) e1
INNER JOIN
(
SELECT personid FROM exams WHERE score >= 80 AND id = '2'
) e2
ON
e1.personid = e2.personid
I believe your select statement should use an OR:
SELECT * FROM exams
WHERE (id = '1' AND score >= 80) OR (id = '2' AND score >= 80)
Try this:
SELECT * FROM exams
WHERE (id = '1' OR id = '2') AND score >=80
Because you cannot have id ='1' and id = '2' simultaneously the number of values returned in your case is 0.