MYSQL - select id from A where (...) AND id from B=1 [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a problem with MySQL. I have 2 tables:
TABLE A
id | txt
1 | abc
2 | bcd
3 | cde
TABLE B
id | accept
1 | 1
2 | 0
3 | 1
I want to display txt (from table A) only if related record (the same id) accept=1 (from table B).
I can do this by setting 2 MySQL queries, but I would like to make it by one MySQL query.

Based on your comment:
SELECT txt
FROM A
LEFT JOIN B
ON A.id=B.id
WHERE B.accept = 1

SELECT A.txt
FROM A INNER JOIN B ON A.id = B.id
WHERE B.accept= 1

Related

Sorting mysql row data to get only a specific id [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a table as shown below
id | t_id | u_id
---+------+------
1 | 2 | 2
2 | 1 | 2
3 | 1 | 1
4 | 2 | 2
5 | 1 | 1
I am trying to get all t_id with u_id of 2 but once without the t_id ever having a u_id of 1 in the history of the whole table.
I tried
SELECT
C_Name, count(*) as count
FROM tenter
WHERE C_Date = '20200127' AND L_TID = '2';
But this gives me the record of all L_TID = 2 and does not filter out those with previous record of L_tid = 1.
Expected result: get all U_ID without the previous history of L_TID = 1, it should get only those without ever having L_tid =1.
Thanks in advance.
One method is aggregation and a having clause:
select t_id
from tenter t
group by t_id
having sum(u_id = 2) > 0 and -- has "2"
sum(u_id = 1) = 0; -- does not have "1"
If you have another table of t values, then exists/not exists might be more efficient:
select t.t_id
from t
where exists (select 1 from tenter tt where tt.t_id = t.t_id and tt.u_id = 2) and
not exists (select 1 from tenter tt where tt.t_id = t.t_id and tt.u_id = 1);
I think you want not exists:
select t.*
from tenter t
where
u_id = 2
and not exists (
select 1 from tenter t1 where t1.t_id = t.t_id and t1.u_id = 1
)
You can also use aggregation, if you just want a list of t_ids. If 1 and 2 are the only possible values, you can just do:
select t_id
from tenter t
group by t_id
having min(u_id) = 2
If there are other possible values:
having max(u_id = 1) = 0 and max(u_id = 2) = 1

SQL How to select latest records with specific value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I trying to find answer by my own, but i cant do it.
I have table:
Id | val
1 | 4
2 | 5
3 | 4
4 | 6
5 | 4
I want to select last 2 IDs with value 4.
Output should be
Id | val
3 | 4
5 | 4
The #Fahmi's query might be improved a bit:
SELECT id, val
FROM (
SELECT id, val
FROM your_tablename
WHERE val = 4
ORDER BY `id` DESC
LIMIT 2
) AS t
ORDER BY t.id ASC
Demo - http://sqlfiddle.com/#!9/eb1227/1
You can try the below -
select id, val from tablename
where val=4
order by id desc limit 2

Mysql Query for two tables with null values [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Have two tables one is customer and other prize amount
**Customer** **PrizeAmount**
*CustomerId Amount* *CustomerId PrizeAmt*
1 500 1 2000
1 2000
Resultant query should be like this
CustomerId Amount PrizeAmt
1 500 2000
1 (NULL) 2000
So how to write query for above result
With left join you can do it
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
so
select customer_id as custo_id,amount,prize_amt
from customer
left join prize_amount on customer.customer_id=prize_amount.customer_id
Left Join
Perhaps this
DROP TABLE IF EXISTS T,T1;
CREATE TABLE T (CID INT , AMOUNT_INVESTED INT);
CREATE TABLE T1 (CID INT, AMOUNT_WON INT);
INSERT INTO T VALUES (1,500);
INSERT INTO T1 VALUES (1,2000),(1,2000);
SELECT CID,AMOUNT_INVESTED,AMOUNT_WON
FROM
(
SELECT T.CID,IF(T.CID <> #P, T.AMOUNT_INVESTED,NULL) AMOUNT_INVESTED, T1.AMOUNT_WON,
#P:=T.CID
FROM T
JOIN T1 ON T.CID = T1.CID
CROSS JOIN (SELECT #P:=0) P
) S
+------+-----------------+------------+
| CID | AMOUNT_INVESTED | AMOUNT_WON |
+------+-----------------+------------+
| 1 | 500 | 2000 |
| 1 | NULL | 2000 |
+------+-----------------+------------+
2 rows in set (0.00 sec)

How to make count of similar data in sql? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table tmp.
Query:
select * from tmp
I want the result in following way:
customer_id | subscriber_id | totalSubscribers
320 | 433 | 3
320 | 434 | 3
Can you tell me how to achieve this?
Here is solution for you question
SELECT customer_id , subscriber_id , count(*) AS totalSubscribers
FROM `tmp` GROUP BY 1,2
or
SELECT customer_id , subscriber_id , count(*) AS totalSubscribers
FROM `tmp` GROUP BY customer_id , subscriber_id
Here is screnshot for executed query
Here is the answer
SELECT customer_id , subscriber_id , count(*) as [TOTAL]
FROM tmp
GROUP BY customer_id , subscriber_id

how to fetch data from multiple table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
what i want is all the record from tbmedAssign where zoneid or zone name given as parameter.
how to wrote query for this?
please help.
table name
tablzone
zone_id(PK) ZoneName
----------- --------
1 east
2 west
3 north
4 south
tbluser
usrId(PK) userzoneId(FK to tblzone) username
-------- ------------------------- ------------
1 1 manish
2 3 rahul
3 2 ankit
4 4 amir
5 2 rashmi
6 1 akash
tbldoctor
docId(PK) usrId(Fk to tbluser) docname
-------- -------------------- ------------
1 2 hemant
2 2 chintu
3 3 rahim
4 1 salman
5 3 kishor
6 3 saurabh
7 2 banti
tblmedAssign
transId(Pk) doctorId(FK to tbldoctor) medId(FK) dateInsert
---------- ------------------------- ------ -----------
1 2 2 20/12/2012
2 3 3 21/12/2012
3 2 3 23/12/2012
4 4 1 24/12/2012
tblmedia
medid(PK) medianame
--------- ---------
1 casfung
2 inem
3 media1
4 tplan
5 casfung test
i want all the record from tblmedAssign where doctor belongs to particular user in tbluser and in the tbluser user is belongs to particular zone and zone id is provided as parameter? for example zoneid= 1;
i want to select medianame too in the record
Basically you need to join the four tables with their linking columns. Try this,
SELECT a.*,
b.*,
c.*,
d.*,
e.*
FROM tblmedAssign a
INNER JOIN tblDoctor b
ON a.doctorID = b.docID
INNER JOIN tblUser c
ON b.usrID = c.usrID
INNER JOIN tblZone d
ON c.userzoneID = d.zone_ID
INNER JOIN tblmedAssign e
ON e.medid = a.medid
WHERE d.zone_id = #zone_id OR -- supply value here
d.zoneName = #zoneName
By JOINing the three tables, something like:
SELECT -- what you want to select
FROM tblmedAssign ta
LEFT JOIN tbldoctor td ON ta.doctorId = td.docId
LEFT JOIN tbluser tu ON td.usrId = tu.usrId
LEFT JOIN tablzone tz ON tu.userzoneId = tz.zone_Id
WHERE tz.zone_Id = #zoneIdParam
select *
from tblmedAssign
inner join tbldoctor on tbldoctor.docId = tblmedAssign.doctorId
inner join tbluser on tbluser.usrId = tbldoctor.usrId
inner join tablzone on tablzone.zone_id = tbluser.userzoneId
where tablzone.ZoneName = 'east'
or tablzone.zone_id = 2
This should serve your purpose
select *
from
tblmedAssign, tbldoctor, tbluser, tablzone
where
tblmedAssign.doctorId = tbldoctor.docId and
tbldoctor.usrId = tbluser.usrId and
tbluser.userzoneId = tablzone.zone_id and
(tablzone.zone_id = x or tablzone.ZoneName = 'y')