MySQL _ Retrieve multiple rows and columns in single query - mysql

am new to Mysql
I want to retrieve all the columns containing StrainName=M18 from the database , but am getting error. please help me in this
SELECT *
strain.strainName,
feature.contigId,
feature.startPosition,
feature.stopPosition,
feature.orfId,
feature.orfType,
feature.funcClassification,
feature.rastId,
feature.strand
from feature,strain
where feature.id=strain.id and strainName='M18';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'strain.strainName,feature.contigId,feature.startPosition,feature.stopPosition,fe' at line 1

you need add comma after *
SELECT *, -- <<====== HERE
strain.strainName,
feature.contigId,
feature.startPosition,
feature.stopPosition,
feature.orfId,
feature.orfType,
feature.funcClassification,
feature.rastId,
feature.strand
from feature,strain
where feature.id=strain.id and
strainName='M18';

to get all columns:
SELECT * from feature,strain
where feature.id=strain.id and strainName='M18';
to get specific columns :
SELECT
strain.strainName,
feature.contigId,
feature.startPosition,
feature.stopPosition,
feature.orfId,
feature.orfType,
feature.funcClassification,
feature.rastId,
feature.strand
from feature,strain
where feature.id=strain.id and
strainName='M18';

Related

Create a SQL view with conditions

I wanted to create a view that selected animal_id, date from service_records table with conditions of Canine type and bath service, but I got an error message:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' service_type='B'.
How should I fix it?
Here is my code:
CREATE VIEW dogs_bath_records
AS
SELECT animal_id, date
FROM service_records
WHERE animal_type = 'Canine', service_type = 'B';
Use AND for multiple conditions
CREATE VIEW dogs_bath_records AS
SELECT animal_id, date
FROM service_records
WHERE animal_type='Canine'
AND service_type='B';

MySQL syntax error when using sqlite3 query

I just wanted to switch from sqlite3 to using MySQL but I get errors in this query:
SELECT
metapp_notif.id,
name,
age,
place,
note,
metapp_notif.lat,
metapp_notif.longt,
haslatlong,
dati,
ntype,
grpm_id,
image,
send_id,
pro.latitude,
pro.longtitude,
metapp_notif.dati,
metapp_notif.activity
FROM (metapp_notif
join (metapp_profil
join metapp_userlocation
ON metapp_profil.user_id = metapp_userlocation.user_id) AS pro
ON metapp_notif.send_id = pro.user_id)
WHERE metapp_notif.rec_id =% d;
I'm getting this error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'pro on metapp_notif.send_id=pro.user_id) where
metapp_notif.rec_id=2' at line 1
I was searching for differences between sqlite3 and mysql but can't figure out what is wrong.
Thanks in advance!
Try this syntax in Mysql
SELECT metapp_notif.id,
name,
age,
place,
note,
metapp_notif.lat,
metapp_notif.longt,
haslatlong,
dati,
ntype,
grpm_id,
image,
send_id,
latitude,
longtitude,
metapp_notif.dati,
metapp_notif.activity
FROM metapp_profil
join metapp_notif
ON metapp_notif.send_id = metapp_profil.user_id
join metapp_userlocation
ON metapp_profil.user_id = metapp_userlocation.user_id
WHERE metapp_notif.rec_id like '% d'; -- Not sure what you are trying to do here

SQL execution error

I try this query
INSERT INTO shop.product(prod_id,model,desc) SELECT product.id_prod,prod_lang.name,product.ref from product left join product_lang on product_lang.id_prod = product.id_prod
However I got this error
SQL execution error # 1065.Response from the database:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near') SELECT product.id_prod,product_lang.name,product.ref from' at line 1
Two problems:
DESC is a reserved keyword. Use backquote (``) for desc.
Change prod_lang to product_lang in the query.
Solution:
INSERT INTO shop.product (prod_id,model,`desc`)
SELECT product.id_prod,product_lang.name,product.ref
from product left join
product_lang on product_lang.id_prod = product.id_prod
Note:
It is a good practice to use backquotes for all columns eventhough it is not a reserved keyword.

using (-) dash in mysql table name

There is an error while i want to backup my database
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-01-000001' at line 1
SELECT * FROM temp_01-01-000001
Filename: F:\xampp\htdocs\erp_zaara\system\database\DB_driver.php
Line Number: 330
Is there any way to solve this prob with the table name (temp_01-01-000001).
You have to add quotes as your table name contains number. I think the following query will work.
SELECT * FROM `temp_01-01-000001`
You could edit line 132 of the file /system/database/drivers/mysql/mysql_utility.php
From:
$query = $this->db->query("SELECT * FROM $table");
To:
$query = $this->db->query("SELECT * FROM `$table`");

Using COUNT and DISTINCT keywords together to count distinct values

I am writing the query to count the number of distinct values in the database table. So I wrote the query as:
select count ( distinct user_id ) as unique_users from 1_time_access;
I am getting error
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'distinct user_id ) as unique_users from 1_time_access' at line 1
Can anyone explain what am I missing?
Remove the space between count and the open parenthesis.
select count(distinct user_id) as unique_users from 1_time_access;