MYSQL multiple tables in one query - mysql

There are two tables named Customers and Payments.
They both have the "CustomerNumber"
Here's what i am trying to do.
Select checkNumber, amount, CustomerNumber, CustomerName
FROM Payments, Customers
And i get an error saying : Unknown column....` in 'field list'
I also tried doing this query
Select checkNumber, amount, Payments.CustomerNumber, CustomerName
FROM Payments, Customers
It didn't work T_T
i tried this one
Select checkNumber, amount, customerNumber, customerName
FROM payments, customers
I get this error "Column 'customerNumber' in field list is ambiguous"

This error happends when there're 2 columns with the same name in 2 tables, so you have to specify the same column in which table, i.e :
Select checkNumber, amount, Customers.CustomerNumber, CustomerName
FROM Payments, Customers
or try to make all of your table name and column quoted in ` like this:
Select `checkNumber`, `amount`, `Payments.CustomerNumber`, `CustomerName`
FROM `Payments`, `Customers`

Are your tables named 1 and 2?
If they are, then mysql is probably not recognizing 1 and 2 as table names, but as numbers. Try enclosing the table names with backticks:
select `1`.CustomerName, lastName, street, state
from `1`, `2`
By the way, this will give you every possible combination of rows... be careful (or use a join)
UPDATE
Given the new data in your comment:
Check the field names... field names must be written exactly as the names in the table(s). Notice that you're writing payments.customerNumber in the select section and payments.customersNumber in the from section

Related

How to check if all entries for a column in sql table is null?

I have a query like this:
SELECT
pid, first_name, last_name, middle_name, hcc_score (.. and so on)
FROM (patient AS p JOIN patient_score AS s ON p.patient_id=s.patient_id);
Before running this query I need to write another query where I need to check if all the entries for a column X are null. This can be checked for multiple columns like first_name, last_name. If all the entries in a specific column are null, then I will mark this column as unavailable (hide it from my frontend table). If any of the entry is not null, then I will mark that column as available and display it in my table.
So far what I've tried is this but I don't know if its the best way to go as the query needs to be as quick as possible, also don't know how to make it work for multiple columns.
SELECT
SUM(CASE WHEN first_name IS NULL THEN 0 ELSE 1 END) AS sum_of_nulls1
FROM (patient AS p JOIN patient_score AS s ON p.patient_id=s.patient_id);

MYSQL query to Select the first duplicate record in JAVA

I am trying to retrieve the the first row among the duplicate row, THE FIRST OCCURED ***
--Table--
Order_No Product User
1 Book Student
2 Book Student
3 Book Student
I want to get the Order_No of the first duplicate row in JAVA, I have used DISTINCT and DISTINCT TOP 1 etc but nothing worked, NEED HELP
SELECT min(order_no), product, user
FROM 'table'
GROUP BY user, product
This is basic SQL?
SELECT min(order_no), product, user FROM table GROUP BY product, user
See also more information on GROUP BY
All fields not part of your group by must have some sort of way to determine which to pick of the n potentially different values. min() will pick the lowest value (even with strings and dates) while max() will pick the highest. You can also use First() and Last() to grab the value according to when they show up.
Supposing you had other values to pick from, you might see something like:
SELECT min(order_no), product, user, min(creation_date),
sum(quantity), first(billing_address)
FROM orders GROUP BY product, user
SELECT t.*
FROM table t
WHERE NOT EXISTS ( SELECT a
FROM table t2
WHERE t2.Product = t.Product
AND t2.User = t.User
AND t2.Order_No < t.Order_No
)

Comparing corresponding rows from the same column and same table

My table structure is:
Customers(customerid,first name,last name,state)
I want to print the name of the customers who belong to the same state and if they are from states where no other customer lives then that customer should be omitted..I tried inner joins but couldn't get the exact results I get one or more extra rows.
The following is from Access SQL, which should work fine for you.
Select customer_id, state, last_name, first_name
FROM Customers
WHERE (((state) In (Select state FROM Customers GROUP BY state HAVING (((Count(state))>1)))))
ORDER BY state,last_name, first_name

create view with two tables that have same columns

I am trying to create a view with the following results. I can create the first part just fine but the second part is where I am having trouble. When I do the join I am receiving an Error Code: 1052. Column 'customer_id' in field list is ambiguous. Now I know that the in the two tables I have duplicate columns.
My question is how do I write the view to ignore one of the duplicate columns
Create a view named customer_addresses that shows the shipping and billing addresses for each customer. This view should return these columns from the Customers table:
customer_id
email_address
last_name
first_name.
This view should return these columns from the Addresses table:
bill_line1
bill_line2
bill_city
bill_state
bill_zip
ship_line1
ship_line2
ship_city
ship_state
ship_zip
The rows in this view should be sorted by the last_name and then first_name columns.
Here is my code.
CREATE OR REPLACE VIEW customer_addresses
AS
SELECT customer_id, email_address, last_name, first_name, addresses.line1
FROM customers JOIN addresses
ORDER BY last_name
I would just identify all the columns like this:
CREATE OR REPLACE VIEW customer_addresses
AS
SELECT
customers.customer_id,
customers.email_address,
customers.last_name,
customers.first_name,
addresses.line1
FROM customers JOIN addresses
ORDER BY customers.last_name
Because I believe that you have an customer_id in the customers table and one in the addresses table. Right?
If you do not specify the table name and add a column to one of the tables with the same column name as in the view the problem will arise again.

Problems Creating table

I want to create a table and populate it with records. the new table should be named majorlist and should include the student ID, the student name ( first and last names concatenated with a space in between), major and the age (In whole years) of each student. label the output columns SID, Name, Major, and Age.
create table majorlist
select studentid as 'SID' from students
select concat(firstname,' ',lastname) as "name" from students
select major as 'major' from students
select round((datediff(now(),DOB))/365) as "age" from students;
I know each one of these works separately but I cant figure out how to integrate them into a table without getting a error. I try removing the select statments from each one and still that doesnt work.
create table majorlist
select studentid as 'SID',
concat(firstname,' ',lastname) as "name",
round((datediff(now(),DOB))/365) as "age"
from students;
Yes the answer by #juergen d is good. You are creating the table by fetching a single table values students.
Then its better to use a single select statement for fetching. You can use the query like -
create table majorlist
select studentid as 'SID',
concat(firstname,' ',lastname) as 'name',round((datediff(now(),DOB))/365) as 'age'
from students;