Selecting Multiple Fields in a Find Statement SQL - mysql

So basically, I am trying to select a row from my database, based on two values, I have tried doing
SELECT FROM users WHERE first = 'Bob' AND last = 'Stevenson';
but when I do it it gives this error:
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 'FROM users WHERE first = 'Robert' AND last = 'Westbury'' at line 1
Thanks in advance,
Robert

You need to specify what column(s) do you want to select, SELECT statement should be followed by the column names or by * to select all the columns:
SELECT * FROM users WHERE first = 'Bob' AND last = 'Stevenson'

Dude, it's Select (* OR <column_name_1>, <column_name_2>... ) From <table_name> where <condition>;

Related

How to use sum, join, order by and where clause in mysql?

So I have two tables like given below:
tb_custmap: idCust, idUser
tb_cust: idCust, revenue
I want to get the revenue from tb_cust based on the idCust and only from specific idUser. I've tried this:
SELECT tb_cust.revenue
FROM tb_custmap INNER JOIN
tb_cust
ON tb_custmap.idCust = tb_cust.idCust
ORDER BY tb_cust.revenue
WHERE idUser='".$AccMgrID."'
But I got error and it is said
"You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near 'WHERE idUser='azkyath'' at line 1 "
Please someone help me ive been doing this for 2 days and still can't get the right one.
Hope this will help you :
Do like this with the use of CI query builder
/*if you want to use `sum` use this instead of select
$this->db->select_sum('tb_cust.revenue');
*/
$this->db->select('tb_cust.revenue');
$this->db->from('tb_custmap');
$this->db->join('tb_cust' , 'tb_cust.idCust = tb_custmap.idCust');
/*In where clause replace `table` name also with the correct table name
from where `idUser` column belongs to
*/
$this->db->where('table.idUser', $AccMgrID);
$this->db->order_by('tb_cust.revenue','ASC');
$query = $this->db->get();
if ( $query->num_rows() > 0 )
{
print_r($query->result());
}
For more : https://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data
You should write the query like this:
SELECT c.revenue
FROM tb_custmap cm INNER JOIN
tb_cust c
ON cm.idCust = c.idCust
WHERE ?.idUser = :AccMgrID
ORDER BY c.revenue;
Notes:
WHERE goes after FROM and before ORDER BY.
Use table aliases that are the abbreviation of the table name.
Qualify all column names (i.e., use the table alias).
The ? is for the table where idUser comes from.
:AccMgrID is for a parameter. You should learn how to use parameters if you are going to use SQL effectively from a programming language.

insert count from another table onto master table

I've got one master table and several additional tables. Each row is a individual person's record.
I want to insert an individual's counts from the additional table as new columns onto the master table. There is a unique ID that can link the two.
I found these instructions: MySQL: UPDATE table with COUNT from another table?
My code is:
SELECT * FROM leeds.salesforce_contacts as allmemcomb
LEFT OUTER JOIN leeds.leenk_ladder_history as ladhist on allmemcomb.salesforce_id = ladhist.member_id
LEFT OUTER JOIN leeds_so.leenk_ladder_config as ladconf on ladhist.member_id = ladconf.ladder_config_id
UPDATE allmemb
set count = (
select count (ladder_change)
from ladhist where ladhist.member_id = allmembcomb.salesforce_id
);
But I'm getting the following error:
Error Code: 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 'UPDATE allmemb set count = ( select count (ladder_change) from ladhist wher' at line 13
thoughts?
You are missing the semicolon at the end of the statement before UPDATE.

How to resolve wrong syntax near HAVING COUNT DISTINCT?

This it the code I am trying to execute:
SELECT ID_K
FROM koncert,
programi
WHERE koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT (DISTINCT programi.Salla) = 2
It returns this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to
your MariaDB server version for the right syntax to use
near 'DISTINCT programi.Salla)=2 LIMIT 0, 25' at line 4.
Tried to change different things but it still won't work .
You should use the count(DISTINCT programi.Salla
) and not count (..) ..remove space between COUNT and (...
SELECT koncert.ID_K
FROM koncert
INNER JOIN programi on koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT(DISTINCT programi.Salla) = 2
but you need also tablename for avoid ambiguity and use explicit join sintax too
First you should use qualified name for your column, when column name is same in both table
SELECT ID_K FROM
should be
SELECT programi.ID_K FROM
else, you will get ambiguous column error. Otherwise, your query looks fine except removing extra space when calling COUNT (#spencer already mentioned in comment)
Also, it is good practice to join your table using JOIN (or INNER JOIN, LEFT JOIN etc) keyword, which makes your query more clear and readable.

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.

MySql subquery error with delete statement

I'm learning sql from these wikibooks pages and I'm trying to answer the last question "Remove all boxes from saturated warehouses" using mysql syntax. The following is what I came up with on my own mainly using previous answers from the same page.
delete from Boxes as b
where b.Warehouse in (
select w.Code
from ( select *
from Warehouses) as w
where w.Capacity < ( select count(*)
from Boxes bb inner join Warehouses ww
on bb.Warehouse = ww.Code
group by bb.Contents) ) ;
The query for this question on the site is generating
this solution doesn't work with mysql 5.0.67
ERROR 1093 (HY000): You can't specify target table 'Boxes' for update in FROM clause
That's why I'm using multiple sub queries as a way to come around this mysql message. However, I have an error in my query and therefore it's not running.
Error Code: 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 'as b where b.Warehouse in ( select w.Code
from ( sel' at line 1
Any idea?
I assume Boxes and Warehouse are tables, you gave Boxes an alias and used it on the Warehouse table, that's probably why it didn't work. Maybe you should try with a query after 'delete from'.
I think your warehouses table returns from than one columns (since * is used). Replace * with the column u want to get the output from.