I don't know how to write SQL syntax - mysql

I don't know how to write SQL syntax. For example:
SELECT username
FROM login
WHERE username='SELECT usr FROM employee WHERE status='Activ';
This query gives me an error.
How to write it?

For your specific query, most likely, the subquery will return more than one row - so you need to use IN instead. Also, you don't quote subqueries, but put them in ():
SELECT username
FROM login
WHERE username IN
(select usr from employee where status='Activ');
This being said, you could/should use a join instead for this
SELECT login.username
FROM login
INNER JOIN employee ON login.username = employee.usr
WHERE employee.status = 'Activ';

Related

Searching SQL database and CONCAT data

New to php and sql so i will try to explain:
I have a SEARCH field in PHP and i am trying to search by 'ProposalName' that match with what the user enters.
This prints out fine:
SELECT
rec_proposal.ProposalID,
ProposalName,
Status,
researcher.FirstName,
researcher.LastName,
reviewer.FirstName as revFirstName,
reviewer.LastName as revLastName,
reviewer.UserID as revUserID,
review.ReviewDate as revDate,
rec_proposal.DateSubmitted
FROM rec_proposal
INNER JOIN User AS researcher
ON rec_proposal.userid = researcher.UserID
LEFT JOIN review
ON rec_proposal.ProposalID=review.ProposalID
LEFT JOIN User as reviewer
ON review.UserID=reviewer.UserID
But now using all the columns I need the above code to do something like this
SELECT * FROM rec_proposal WHERE CONCAT (ProposalName) LIKE'%test%'
SO if user enters the word 'test' you would see ProposalName that contains the words test
Just add your WHERE clause, it should work. And as scaisEdge noted in their comment, you don't need CONCAT() if you are just evaluating a single column :
SELECT
rec_proposal.ProposalID,
ProposalName,
Status,
researcher.FirstName,
researcher.LastName,
reviewer.FirstName as revFirstName,
reviewer.LastName as revLastName,
reviewer.UserID as revUserID,
review.ReviewDate as revDate,
rec_proposal.DateSubmitted
FROM rec_proposal
INNER JOIN User AS researcher
ON rec_proposal.userid = researcher.UserID
LEFT JOIN review
ON rec_proposal.ProposalID=review.ProposalID
LEFT JOIN User as reviewer
ON review.UserID=reviewer.UserID
WHERE rec_proposal.ProposalName LIKE '%test%'

Consults with subqueries and use of regexp

I am trying to identify users that can receive medicines produce interactions. For that I have used this code:
SELECT c.user, COUNT(DISTINCT c.user)
FROM mytable AS c
JOIN (SELECT id_beneficiario, mes
FROM mytable
WHERE codigo_atc REGEXP 'C01BD01|N06AA09|J01FA10|J01MA02|J01FA09|N05AH02|L01XE06|R06AA02|A03FA03|
L04AA27|J02AC01|N06AB03|C03CA01|N05AD01|C03AA03|J02AB02|J01MA12|N05AN01|J01XD01|J05AE04|L01XE08|
N05AH03|N05AH04|N05AX08|J05AE03|J05AE01|C07AA07|L04AD02|M03BX02|N06AX05|J01EE03') AS d
ON c.user = d.user AND c.mes = d.mes
WHERE (c.codigo_atc REGEXP 'C01BD01|N06AA09|J01FA10|J01MA02|J01FA09|N05AH02|L01XE06|R06AA02|A03FA03|
L04AA27|J02AC01|N06AB03|C03CA01|N05AD01|C03AA03|J02AB02|J01MA12|N05AN01|J01XD01|J05AE04|L01XE08|
N05AH03|N05AH04|N05AX08|J05AE03|J05AE01|C07AA07|L04AD02|M03BX02|N06AX05|J01EE03')
GROUP BY c.user;
I am working local and the consult take too much time and apper the next Error: Error code: 2013. Lost conection to MYSQL server during query. Would be possible optimize my code to avoid the error?
Can this work for you?
WHERE codigo_atc IN ('C01BD01','N06AA09','J01FA10','J01MA02','J01FA09',
'N05AH02','L01XE06','R06AA02','A03FA03','L04AA27',
'J02AC01','N06AB03','C03CA01','N05AD01', etc)
This IN clause will return true if codigo_atc matches any of the values in the (list).
SQL is far better at set logic than it is at regular expression matching.
You can try with a in instead of a regexp..
Could be this is more performant
SELECT c.user, COUNT(DISTINCT c.user)
FROM mytable AS c
JOIN (SELECT id_beneficiario, mes
FROM mytable
WHERE codigo_atc IN ('C01BD01','N06AA09','J01FA10','J01MA02','J01FA09','N05AH02','L01XE06','R06AA02','A03FA03','
L04AA27','J02AC01','N06AB03','C03CA01','N05AD01','C03AA03','J02AB02','J01MA12','N05AN01','J01XD01','J05AE04','L01XE08','
N05AH03','N05AH04','N05AX08','J05AE03','J05AE01','C07AA07','L04AD02','M03BX02','N06AX05','J01EE03') AS d
ON c.user = d.user AND c.mes = d.mes
WHERE c.codigo_atc IN ('C01BD01','N06AA09','J01FA10','J01MA02','J01FA09','N05AH02','L01XE06','R06AA02','A03FA03','
L04AA27','J02AC01','N06AB03','C03CA01','N05AD01','C03AA03','J02AB02','J01MA12','N05AN01','J01XD01','J05AE04','L01XE08','
N05AH03','N05AH04','N05AX08','J05AE03','J05AE01','C07AA07','L04AD02','M03BX02','N06AX05','J01EE03')
GROUP BY c.user;
but if you explain your related table schema and what you want obtain could is possible write a more simple query

How to Add User Input Criterion to an Access Inner Joined Crosstab Query

Prompting for user input parameters in an Access query is fairly straight forward. Go to design view and under criteria, just put [Your Text:] -- and when the query runs the user is prompted for the field criteria.
Unfortunately when I try this with my Inner Joined Crosstab query I get the error:
The Microsoft Access engine does not recognize the '[Your Text:]' as a
valid field name or expression.
I know the inner join crosstab still allows for criteria, as hardcorded criterion works.
To give you a better idea of what I'm looking at, here is the SQL code.
DOESN'T WORK:
SELECT *
FROM ([CrossTabQ1]
INNER JOIN [CrossTabQ2] ON [CrossTabQ1].[Month] = [CrossTabQ2].[Month])
INNER JOIN [Query3] ON [CrossTabQ1].[Month] = [Query3].[Month]
WHERE ((([CrossTabQ1].[Month])= [Enter Month, in YYYY-MM Format:]))
ORDER BY [CrossTabQ1].[Month];
DOES WORK:
WHERE ((([CrossTabQ1].[Month])="2015-12"))
ORDER BY [CrossTabQ1].[Month];
Any tips regarding why I'm getting this error and how I can accept user input criterion would be greatly appreciated!
Try:
PARAMETERS Enter_Month Text ( 255 );
SELECT *
FROM ([CrossTabQ1]
INNER JOIN [CrossTabQ2] ON [CrossTabQ1].[Month] = [CrossTabQ2].[Month])
INNER JOIN [Query3] ON [CrossTabQ1].[Month] = [Query3].[Month]
WHERE ((([CrossTabQ1].[Month])=[Enter_Month]))
ORDER BY [CrossTabQ1].[Month];

How do I put a previous result into the next query? MYSQL

Okay so, this is my query.
select id from rooms where owner = 'oknow';
and the answer I get is
325
However, I made another SQL within this one as below
update users set home_room = 'mysql_fetch_assoc()' where username = 'omarisgod';
I want the 'mysql_fetchassoc()' to be the '325' value, how do I do this?
A subquery will do this:
UPDATE users SET home_room = (SELECT id FROM rooms WHERE owner = 'oknow') WHERE username = 'omarisgod';
You can conceptualize it thusly: The query inside parentheses will return a result, which will be utilized by the outer query.

specific select work only when i add db name

I'm using SQL Server 2008 Express
All of the select statments are fine. Now I have this one:
SELECT
ORG.id, ORG.img, ORG.name, ORG.city, ORG.address,
ORG.zip, ORG.telephone, ORG.telephone2,
ORG.fax, ORG.email, ORG.vaname, ORG.vanumber,
ORG.yor_photo, ORG.commission,
Clients.id AS yor_id, Clients.prefix,
Clients.fname, Clients.lname, Clients.phone,
Clients.pelephone, Clients.email, Clients.pid,
ORG.adddate, ORG.note
FROM Org
LEFT OUTER JOIN
(select * from Clients where yor = 1) as Clients ON Clients.company = ORG.id
WHERE ORG.id=" & ORGID
It is not working, I get error "invalid object name"
If I add the DBNAME.DBO in front of the table name it works
The problem is that i don't want to change that on every project
Why is it not working?
UPDATE
the problem is not with the db name, the problem is with the AS yor_id in the select statment.
if i remove it the record retrieved is not full with all the data but if i write it the data is full but yor_id is empty
UPDATE
NEVER MIND, my bad! id column was corupted
Before that query you need to write
use DBNAME
in order to switch to your dbname.
Probably you are running that query on master database, so just do this and should work:
use DBNAME
SELECT
ORG.id, ORG.img, ORG.name, ORG.city, ORG.address,
ORG.zip, ORG.telephone, ORG.telephone2,
ORG.fax, ORG.email, ORG.vaname, ORG.vanumber,
ORG.yor_photo, ORG.commission,
Clients.id AS yor_id, Clients.prefix,
Clients.fname, Clients.lname, Clients.phone,
Clients.pelephone, Clients.email, Clients.pid,
ORG.adddate, ORG.note
FROM Org
LEFT OUTER JOIN
(select * from Clients where yor = 1) as Clients ON Clients.company = ORG.id
WHERE ORG.id=" & ORGID