Select values which are not in other table [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
Let's say i have two tables.
Table client
ID - primary key ( 1,2,3,4,5,6)
Table Orders
OrderID - primary key
ClientID (1,2,4,5)
I need to get ROWS of table CLIENT of clients 3 and 6 (which don't have orders / are not in orders table)

This is basic query. Try your self.
select * from client where id not in (select ClientID from orders)

Related

How to select from table A only where a relationship between the table A row and a specific row in B exists? [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
lets say I have two MySQL tables, table A and table B. Each table has a primary key called id and a name column. I also have a third table, table C, that contains relationships between table A and table B. Each row contains two foreign keys called a_id and b_id, which, as you might expect, correspond to ids in tables A and B.
What I want to do is select a random set of 10 table A rows, but only select rows that have a relationship with specific entries in table B. I don't know which entries I'm looking for ahead of time, and I will start with their names. The names will be provided via query parameters.
I understand I should probably start with this:
SELECT * FROM `A`
ORDER BY RAND()
LIMIT 10
But I don't know how to structure the where clause.
You need something like this:
SELECT *
FROM `A` a
INNER JOIN `C` c ON
a.ID = c.a_id AND
c.b_id in (1,2,3,4) -- your entries here
-- order and limit as you wish
ORDER BY RAND()
LIMIT 10

selecting a table in mysql [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 4 years ago.
Improve this question
I have a mysql database and some tables.
One table is Products: Its columns are prod_id, prod_type.
Prod_types are mobile or laptop or book.
Each of them have different attributes (eg: publisher, author for books, battery_condition, model for laptop etc).
Should I store all the data in the Products table by adding columns like 'author', 'model' etc and keep null for those columns which don't apply to my product.
For example for an entry with prod_type='book' , 'model', 'batter_condition' etc will be null. Or should I create different tables for 'book', 'mobile' and 'laptop' ?
Yes. Create new tables for Book, Mobile and Laptop. Have the product_id attribute in each of those tables. This product_id attribute is a foreign key to the primary key of table products, which is product_id as well.
Storing all data in products will result in a lot of NULL fields and will make the data storage inefficient.

How to rearrange the column position in MySQL [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
How can I change the column position to first using ALTER query in MySQL table?
If you want your column at first position, you should use
ALTER TABLE tableName MODIFY COLUMN yourColumnName varchar(50) FIRST
For example, if your table is
Employee{FirstName,LastName,EmpId}
And if you want to move EmpId to first position
ALTER TABLE Employee MODIFY COLUMN EmpId varchar(15) FIRST;
For example :
Below query will bring empname column after department :
ALTER TABLE Employees MODIFY COLUMN empName VARCHAR(50) AFTER department;

Insert a value in a column seperate by comma [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
Insert a value in a column name e-mail then it display all emails seperated by comma. This email is save at different time but for same id.
Is it possible then how?
You can use GROUP_CONCAT to show the comma separated records that belong to same group,be aware of that fact GROUP_CONCAT has a default limit of 1024 characters to concat but it can be increased as mentioned in docs
CREATE TABLE Table1
(id INT ,`test` varchar(25))
;
INSERT INTO Table1
(id,`test`)
VALUES
(1,'test#test.com'),
(1,'test#test.com'),
(1,'test#test.com'),
(2,'test2#test.com'),
(2,'test2#test.com'),
(2,'test2#test.com')
;
SELECT id, GROUP_CONCAT(test) emails
FROM
Table1 GROUP BY id
Fiddle Demo

Database design question [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
I have 2 separate objects (A,B) no relationship between A,B
Object A has (X,Y,Z) values
Object B has (X,Y,Z,P,W) values
Values are different in A and B, for example A.Z = 4 while B.Z = 6
What is the best practice in creating tables:
Create separate tables (A,B) with values
Create table Type (A,B) and Table Value (X,Y,Z,P,W) with key to table Type.
Thanks,
For low cardinality and simple situations I would just go with:
3. Table AB with columns (X, Y, Z, P, W) and an A/B type identifier and let P, W be null for A objects
With more information I might make a different choice.